Difference between = and := in golang

= is an assignment statement := is to declare a type (the compiler does it for you) and assign it.
1. = is used with the var keyword. var can be used inside a function or outside of a function.
   := can only be used in functions, so only local variables can be defined.
2. After defining with var, after assigning with =, it can also be changed. However, variables defined with := cannot change the value (that is, b: = "q", you can't redefine b: = "s" in the function, but b = = "s")

3. var and = can be parallel Define and assign:
For example:
var p, q float32 = 1.1, 2.2

  := can also do this:
b, f := "q", 1

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326888712&siteId=291194637