Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 452709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:05:59+00:00 2026-05-12T22:05:59+00:00

I want to know what is happening here. There is the interface for a

  • 0

I want to know what is happening here.

There is the interface for a http handler:

type Handler interface {
    ServeHTTP(*Conn, *Request)
}

This implementation I think I understand.

type Counter int

func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
    fmt.Fprintf(c, "counter = %d\n", ctr);
    ctr++;
}

From my understanding it is that the type “Counter” implements the interface since it has a method that has the required signature. So far so good. Then this example is given:

func notFound(c *Conn, req *Request) {
    c.SetHeader("Content-Type", "text/plain;", "charset=utf-8");
    c.WriteHeader(StatusNotFound);
    c.WriteString("404 page not found\n");
}

// Now we define a type to implement ServeHTTP:
type HandlerFunc func(*Conn, *Request)
func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
    f(c, req) // the receiver's a func; call it
}
// Convert function to attach method, implement the interface:
var Handle404 = HandlerFunc(notFound);

Can somebody elaborate on why or how these various functions fit together?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:05:59+00:00Added an answer on May 12, 2026 at 10:05 pm

    This:

    type Handler interface {
        ServeHTTP(*Conn, *Request)
    }
    

    says that any type which satisfies the Handler interface must have a ServeHTTP method. The above would be inside the package http.

    type Counter int
    
    func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
        fmt.Fprintf(c, "counter = %d\n", ctr);
        ctr++;
    }
    

    This puts a method on the Counter type which corresponds to ServeHTTP. This is an example which is separate from the following.

    From my understanding it is that the
    type “Counter” implements the
    interface since it has a method that
    has the required signature.

    That’s right.

    The following function by itself won’t work as a Handler:

    func notFound(c *Conn, req *Request) {
        c.SetHeader("Content-Type", "text/plain;", "charset=utf-8");
        c.WriteHeader(StatusNotFound);
        c.WriteString("404 page not found\n");
    }
    

    The rest of this stuff is just fitting the above so that it can be a Handler.

    In the following, a HandlerFunc is a function which takes two arguments, pointer to Conn and pointer to Request, and returns nothing. In other words, any function which takes these arguments and returns nothing can be a HandlerFunc.

    // Now we define a type to implement ServeHTTP:
    type HandlerFunc func(*Conn, *Request)
    

    Here ServeHTTP is a method added to the type HandlerFunc:

    func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
        f(c, req) // the receiver's a func; call it
    }
    

    All it does is to call the function itself (f) with the arguments given.

    // Convert function to attach method, implement the interface:
    var Handle404 = HandlerFunc(notFound);
    

    In the above line, notFound has been finagled into being acceptable for the interface for Handler by artificially creating a type instance out of the function itself and making the function into the ServeHTTP method for the instance. Now Handle404 can be used with the Handler interface. It’s basically a kind of trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change my question. please let me know what's happening; type is
I'm making a chat responder for a game and i want know if there
want to know why String behaves like value type while using ==. String s1
I know there are a lot of questions on this topic already, but it
I know why it's happening but I want to find a way around it
I know there are hundreds of posts on here about htaccess but I can't
I know there are a number of threads on here about how to use
i want know if is possible, to get a specific element value of a
I am currently dabbling in Magento and I want know how I can create
Want to know what the stackoverflow community feels about the various free and non-free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.