Difference between := and = operators in Go

前端 未结 9 1421
野趣味
野趣味 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:11

    := declares and assigns, = just assigns

    It's useful when you don't want to fill up your code with type or struct declarations.

    // Usage with =
    var i int
    var U, V, W float64
    var k = 0
    var x, y float32 = -1, -2
    
    // Usage with :=
    i, j := 0, 10
    f := func() int { return 7 }
    ch := make(chan int)
    

提交回复
热议问题