 2018-03-24 18:18

# Go中的三个逗号语法是什么？

Can anyone explain to me what is this line doing? I've never seen this before, I guess.

``````np.Point, np.Valid = Point{}, false
``````

As stated in this github code

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• dtjxhf595733 2018-03-24 18:20
已采纳

This is not a three comma syntax. It is actually initializing two variables together in a line

``````np.Point, np.Valid = Point{}, false
``````

is similar to

``````np.Point = Point{}
np.Valid = false
``````
点赞 评论
• doupang9080 2018-03-24 18:43

The Go Programming Language Specification

Assignments

A tuple assignment assigns the individual elements of a multi-valued operation to a list of variables. There are two forms. In the first, the right hand operand is a single multi-valued expression such as a function call, a channel or map operation, or a type assertion. The number of operands on the left hand side must match the number of values. For instance, if f is a function returning two values,

`x, y = f()`

assigns the first value to x and the second to y. In the second form, the number of operands on the left must equal the number of expressions on the right, each of which must be single-valued, and the nth expression on the right is assigned to the nth operand on the left:

`one, two, three = '一', '二', '三'`

A tuple assignment assigns the individual elements of a multi-valued operation to a list of variables. In the second form, the number of operands on the left must equal the number of > expressions on the right, each of which must be single-valued, and the nth expression on the right is assigned to the nth operand on the left.

``````np.Point, np.Valid = Point{}, false
``````

Or, equivalently,

``````t1 := Point{}
t2 := false
np.Point = t1
np.Valid = t2
``````
点赞 评论