请教大神们,GO中冒号":"的作用是什么啊?

qq634284568 · 2017-08-07 08:43:15 · 7825 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2017-08-07 08:43:15 的主题,其中的信息可能已经有所发展或是发生改变。

&Client{
    url:       u,
    userAgent: conf.UserAgent,
    httpClient: &http.Client{
        Timeout:   conf.Timeout,
        Transport: tr,
    },
    credentials: conf.Credentials,
}

请问这冒号代表的是什么意思?


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

7825 次点击  
加入收藏 微博
8 回复  |  直到 2018-03-07 09:32:01
channel
channel · #1 · 8年之前

赋值,你可以理解为和 = 类似

qq634284568
qq634284568 · #2 · 8年之前
channelchannel #1 回复

赋值,你可以理解为和 `=` 类似

谢谢

lys861205
lys861205 · #3 · 8年之前

申明变量并赋值

xylong
xylong · #4 · 8年之前

可以这么看,:是申明,=是赋值,所以:=就是申明并赋值

marlonche
marlonche · #5 · 8年之前

Composite literals, 根据已经声明的类型构造复合的变量,类似于 key: value, 应该不是声明变量

type Point3D struct { x, y, z float64 } type Line struct { p, q Point3D custom map[string]int }

l := &Line{ p: Point3D{}, q: Point3D{y: -4, z: 12.3}, custom: map[string]int{ "weight": 1, "color": 0, }, }

The key is interpreted as a field name for struct literals, an index for array and slice literals, and a key for map literals. For map literals, all elements must have a key. It is an error to specify multiple elements with the same field name or constant key value.

For struct literals the following rules apply:

A key must be a field name declared in the struct type. An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared. If any element has a key, every element must have a key. An element list that contains keys does not need to have an element for each struct field. Omitted fields get the zero value for that field. A literal may omit the element list; such a literal evaluates to the zero value for its type. It is an error to specify an element for a non-exported field of a struct belonging to a different package.

qq634284568
qq634284568 · #6 · 8年之前
marlonchemarlonche #5 回复

Composite literals, 根据已经声明的类型构造复合的变量,类似于 key: value, 应该不是声明变量 type Point3D struct { x, y, z float64 } type Line struct { p, q Point3D custom map[string]int } l := &Line{ p: Point3D{}, q: Point3D{y: -4, z: 12.3}, custom: map[string]int{ "weight": 1, "color": 0, }, } The key is interpreted as a field name for struct literals, an index for array and slice literals, and a key for map literals. For map literals, all elements must have a key. It is an error to specify multiple elements with the same field name or constant key value. For struct literals the following rules apply: A key must be a field name declared in the struct type. An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared. If any element has a key, every element must have a key. An element list that contains keys does not need to have an element for each struct field. Omitted fields get the zero value for that field. A literal may omit the element list; such a literal evaluates to the zero value for its type. It is an error to specify an element for a non-exported field of a struct belonging to a different package.

3Q!

qq634284568
qq634284568 · #7 · 8年之前
xylongxylong #4 回复

可以这么看,:是申明,=是赋值,所以:=就是申明并赋值

3Q~

xshrim
xshrim · #8 · 7年之前

结构体/类的初始化方式,初始化类的时候给类的指定成员赋值。

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传