As a Go beginner, I stumbled across code where there are brackets directly after func
func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
So what does (v Version)
mean?
As a Go beginner, I stumbled across code where there are brackets directly after func
func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
So what does (v Version)
mean?
收起
This is not a function but a method. In this case, it adds the MarshalJSON method to the Version struct type.
The v
is the name for the received value (and would be analogous to this in a Java method or self in Python), the Version specifies the type we're adding the method to.
See go by example for, well, an example, and the specification for more details.
报告相同问题?