duanbei7035 2015-09-02 19:42
浏览 1853
已采纳

func之后的括号在Go中意味着什么?

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?

  • 写回答

1条回答 默认 最新

  • dsjq6977 2015-09-02 19:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?