I'm new to the language and so far what I have read doesn't explain what is happening here.
I was looking at JSON decoding.
Here is a function in the package:
func (dec *Decoder) Decode(v interface{}) error
There is similar notation here:
func (dec *Decoder) Buffered() io.Reader
How do these functions access the (dec *Decoder)
? Given that the second function has no parameters, I am guessing this is not something that must be passed directly in the function call but something similar?
These two functions are methods because they are associated with receivers. In each method declaration, (dec *Decoder)
describes the receiver. If you have a *Decoder
called decoder
, you call Buffered
on it like this:
reader := decoder.Buffered()
Take a look at the the Go tutorial lesson on this topic.