Hacker News new | ask | show | jobs
by Nvorzula 3143 days ago
x := 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()

2 comments

That 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.

Interesting. Seems I should take a look at Go.
You also usually declare stuff using this.

For example

    a := "hello"
or

    a := 0
or

    a := []byte{0, 1}