3 ms·
I don't know Go. What does := do?
by maxxxxx 9y ago
I don't know Go. What does := do?
- _sdegutis 9y agoImplies the new variable's type to be the return type of the assigned expression.
- Nvorzula 9y agox := some_code_here says "x is a NEW variable whose type is whatever the result of the expression on the right hand side is". Say we have a function, GetInt64(), which...returns an int64. x := GetInt64() both declares x to be an int64 and places the resulting value of the right hand side into x. If x was already to declared to be an int64, E.G.: var x int64 x := GetInt64() Then the compiler complain that x is not new - it has already been declared. In this case you have to omit the colon to remove the fancy "auto" semantics. var x int64 x = GetInt64()
- maxxxxx 9y agoInteresting. Seems I should take a look at Go.
- baby 9y agoYou also usually declare stuff using this. For example a := "hello" or a := 0 or a := []byte{0, 1}
- memsom 9y agoThat looks a lot like the old Pascal assignment vs equivalence operators. In Pascal (read Delphi) we had '=' for equality and ':=' for assignment. So: x = 5 <-- a boolean statement, true if x equ 5 x := 5 <-- an assignment of the value 5 to the var x. TBH, it's only like '=' vs '==' in C like languages. But it's easier to spot errors, as Pascal can never have a valid statement where the two are used in the wrong way.