I have found many similar problem, but not what I want please see following codes.
(1)
var buf bytes.Buffer
fmt.Fprint(&buf,"test")
(2)
var w http.ResponseWriter
http.Error(w,http.StatusText(http.StatusBadRequest),http.StatusBadRequest)
both http.ResponseWriter and bytes.Buffer implement io.Writer interface, so I think they should have same behavior.
when I change to
http.Error(&w,http.StatusText(http.StatusBadRequest),http.StatusBadRequest)
jetbrains goland tell me
Cannot use '&w' (type *http.ResponseWriter) as type ResponseWriter
I wonder why buf
have '&' can works, but another don't work?
I googled, somebody say when pass value to function, it can reference to &w
but another say if you pass a pointer, it can deference to value
from https://github.com/golang/go/wiki/MethodSets#variables
To make it easier to remember these rules, it may be helpful to simply consider the pointer- and value-receiver methods separately from the method set. It is legal to call a pointer-valued method on anything that is already a pointer or whose address can be taken (as is the case in the above example). It is legal to call a value method on anything which is a value or whose value can be dereferenced (as is the case with any pointer; this case is specified explicitly in the spec).
I am totally confused, I can't understand by myself. I wish you can help me, thank you!