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

为什么go要求您在变量名前添加&?

Here is a snippet that I am looking at:

var t txn
t.c = c
err := c.read(&t.req)

Why do you have to write &t and not just t.req?

You don't always have to use the ampersand. In your example it depends on the signature of c.read, which asks for a pointer (* before the type, such as *MyStruct). & returns the address of a value, giving you a pointer to it, so &t.req satisfies read's signature.

For further reading, see the FAQ on pointers and the spec on Address operators.

近期文章

  • 为什么排序“ tan”!=“ ant”? [关闭]
  • 读取直到服务器关闭连接
  • Go-具有数组和丢失值的递归结构
  • 如何检查Golang项目的大小?
  • 从大文本中删除所有非字母数字字符的有效方法
  • Go-如何将二进制字符串作为文本转换为二进制字节?
  • 在Go中与.go源文件位于同一目录中打开文件
  • 有没有办法让Go一步编译和链接(一次6g和6l)?
  • 如何在Golang上的Google App Engine上建立TCP侦听器或服务器?
  • 读取并合并两个Yaml文件
  • Golang奇怪的格式日期
  • 我可以通过GO中的方法扩展错误接口吗?
  • Golang中的指针
  • Golang时间解析问题
  • 为什么Go的结果相同?
  • 包含字符串文字的数组初始化
  • 从SQL(C#和实体框架)到mongodb(Go和mgo)
  • 忽略标志大小写
  • 在Golang中执行SQL查询
  • 带有for循环的goroutine中的“ Select”语句

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

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

友情链接:代码精华