FAQ
I'm trying to get my web server to accept different socket calls in one
function. My code looks like this:

Go:

func handler(w io.Writer, r *io.ReadCloser) {

//do something

}

func main() {

http.HandleFunc("/", handler)

http.ListenAndServe(":3000", nil)

}

I get the error: cannot use handler (type func(io.Writer, *io.ReadCloser))
as type func(http.ResponseWriter, *http.Request) in argument to
http.HandleFunc


Am I using interfaces wrongly? How do I implement this?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jesse McNelis at Oct 25, 2014 at 3:55 am

    On Sat, Oct 25, 2014 at 12:28 PM, Audrey Lim wrote:
    I'm trying to get my web server to accept different socket calls in one
    function. My code looks like this:

    Go:

    func handler(w io.Writer, r *io.ReadCloser) {

    //do something

    }

    func main() {

    http.HandleFunc("/", handler)

    http.ListenAndServe(":3000", nil)

    }

    I get the error: cannot use handler (type func(io.Writer, *io.ReadCloser))
    as type func(http.ResponseWriter, *http.Request) in argument to
    http.HandleFunc
    Am I using interfaces wrongly? How do I implement this?
    You want a call your handler() function from a http request then
    you'll have to write an actual http.Handler to wrap it.
    eg.

      func handler(w io.Writer, r io.ReadCloser) {
        //do something
      }

      func wrappingHandler(w http.ResponseWriter, r *http.Request){
         handler(w, r.Body)
      }
      func main() {

      http.HandleFunc("/", wrappingHandler)

      http.ListenAndServe(":3000", nil)

      }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 25, '14 at 1:28a
activeOct 25, '14 at 3:55a
posts2
users2
websitegolang.org

2 users in discussion

Jesse McNelis: 1 post Audrey Lim: 1 post

People

Translate

site design / logo © 2022 Grokbase