Re: [go-nuts] I see golang net/http code, i imitate it, but i can't.

93 views
Skip to first unread message

Howl

unread,
Dec 4, 2016, 10:32:56 AM12/4/16
to golan...@googlegroups.com

HandlerFunc is not a struct. You probably wanted to do this (line 17):

demo := HandlerFunc(Add)


On 04/12/16 15:02, Neng Kong wrote:
i see this:
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

and i imitate it, but can't compile:
package main

import "fmt"

type Handler interface {
    Call(x, y int)
}
func Add(x, y int) {
    fmt.Println(x+y)
}
type HandlerFunc func(x, y int)

func (f HandlerFunc) Call(x, y int) {
    f(x, y) 
}
func main() {
    demo := HandlerFunc{}
    var i Handler
    i = demo
    i.Call(1, 2)
}

[john@localhost gweb]$ go run demo.go
# command-line-arguments
./inspiration_by_golang_http.go:17: invalid type for composite literal: HandlerFunc



i do not understand it, this is way ?
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages