About golang net/http code trick, i want to imitate code trick but i can not.

181 views
Skip to first unread message

401...@gmail.com

unread,
Dec 3, 2016, 12:43:49 PM12/3/16
to golang-nuts
I see this code:
type HandlerFunc func(ResponseWriter, *Request)

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

i imitate this but my code can not compile:
[john@localhost 01]$ cat inspiration_by_golang_http.go
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 01]$ go run inspiration_by_golang_http.go
# command-line-arguments
./inspiration_by_golang_http.go:17: invalid type for composite literal: HandlerFunc

Why? I want to know. Thinks!

My computer arch is amd64. OS is linux. Distribution is fc24.



Tamás Gulácsi

unread,
Dec 3, 2016, 2:27:51 PM12/3/16
to golang-nuts
demo := HandlerFunc(func(x,y int) {})

Seb Binet

unread,
Dec 3, 2016, 8:00:10 PM12/3/16
to Tamás Gulácsi, golang-nuts

Or just:
var demo HandlerFunc
?

-s

sent from my droid


On Dec 3, 2016 8:27 PM, "Tamás Gulácsi" <tgula...@gmail.com> wrote:
demo := HandlerFunc(func(x,y int) {})

--
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.

Neng Kong

unread,
Dec 4, 2016, 9:14:44 AM12/4/16
to golang-nuts, tgula...@gmail.com
I replace 
demo := HandlerFunc{}
to
var demo HandlerFunc

also can not
[john@localhost gweb]$ go run inspiration_by_golang_http.go 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x401035]

goroutine 1 [running]:
panic(0x47a9a0, 0xc42000a090)
/home/john/go/src/runtime/panic.go:500 +0x1a1
main.HandlerFunc.Call(0x0, 0x1, 0x2)
/home/john/gweb/inspiration_by_golang_http.go:14 +0x35
main.main()
/home/john/gweb/inspiration_by_golang_http.go:20 +0x40
exit status 2


Maube i can not imitate it



在 2016年12月4日星期日 UTC+8上午9:00:10,Sebastien Binet写道:

Or just:
var demo HandlerFunc
?

-s

sent from my droid

On Dec 3, 2016 8:27 PM, "Tamás Gulácsi" <tgula...@gmail.com> wrote:
demo := HandlerFunc(func(x,y int) {})

--
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.

Tamás Gulácsi

unread,
Dec 4, 2016, 9:27:14 AM12/4/16
to golang-nuts, tgula...@gmail.com
2016. december 4., vasárnap 15:14:44 UTC+1 időpontban Neng Kong a következőt írta:
I replace 
demo := HandlerFunc{}
to
var demo HandlerFunc

also can not
[john@localhost gweb]$ go run inspiration_by_golang_http.go 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x401035]

goroutine 1 [running]:
panic(0x47a9a0, 0xc42000a090)
/home/john/go/src/runtime/panic.go:500 +0x1a1
main.HandlerFunc.Call(0x0, 0x1, 0x2)
/home/john/gweb/inspiration_by_golang_http.go:14 +0x35
main.main()
/home/john/gweb/inspiration_by_golang_http.go:20 +0x40
exit status 2


Maube i can not imitate it


Please start with tour.golang.org, learn the definition of pointer!
In Go everything is initialized with it's type's zereo value - for an function (HandlerFunc is a function), it is nil.
As the runtime kindly says, you try to dereference it by calling it, that results in panic.

So, as I've said, try

    demo := HandlerFunc(func(x,y int) { fmt.Println("Called", x, y) })


Reply all
Reply to author
Forward
0 new messages