What do the brackets after func in go mean?

As a beginner, I came across a code where there are brackets immediately afterfunc

func (v Version) MarshalJSON() ([]byte, error) {
  return json.Marshal(v.String())
}

So what does it mean (v Version)?

+4
source share
1 answer

This is not a function, but a method. In this case, it adds the MarshalJSON method to the version structure type.

v is the name for the value received (and will be similar to that in the Java method or in the Python kernel), the version indicates the type to which we add the method.

For details, see as an example for example, as well.

+8
source

Source: https://habr.com/ru/post/1605625/


All Articles