Difference between := and = operators in Go

前端 未结 9 1403
野趣味
野趣味 2020-12-12 10:45

What is the difference between the = and := operators, and what are the use cases for them? They both seem to be for an assignment?

相关标签:
9条回答
  • 2020-12-12 11:23

    In Go, := is for declaration + assignment, whereas = is for assignment only.

    For example, var foo int = 10 is the same as foo := 10.

    0 讨论(0)
  • 2020-12-12 11:24

    “:=” use to do declaration and initilization at the same time, following is an example.

    Usage of “=”

    var i int

    i = 10

    https://play.golang.org/p/RU88ty_SGa

    Usage of “:=”

    i := 10

    https://play.golang.org/p/XBdjBh-DQB

    0 讨论(0)
  • 2020-12-12 11:28

    Only = is the assignment operator.

    := is a part of the syntax of the Short variable declarations clause.

    0 讨论(0)
提交回复
热议问题