我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

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?

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.

近期文章

  • 在Go中将指针用作地图键是否安全?
  • (继续)如何使用toml文件?
  • 从函数返回语句转到数组切片
  • 附加片性能差..为什么呢?
  • 在ANSI C中实现无溢出系统堆栈
  • 防止元帅在结构的字符串字段中转义引号
  • 意外的分号或换行符,如果没有
  • 频道被封锁时,频道会保持秩序吗?
  • 如何处理“ fmt” golang库软件包进行CLI测试
  • memstats结构中的哪些字段仅引用堆,仅引用堆栈
  • 如何在golang中有效地连接字符串数组和字符串
  • 从接口类型到实际类型的强制类型断言
  • Go项目结构-最佳做法
  • 如何在Golang Beego中解析表单数组
  • Go mgo软件包中的连接池
  • 去除以大数(big.Int)
  • Go运行时使用的线程数
  • 为什么fmt.Println在Google App Engine中不起作用
  • 您如何在流中上传文件?
  • 从protobuf更新数据

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华