Quidest?

Assignment statement in Go

· Lorenzo Drumond

The following statement:

1b = Book{Title: "The Making of a Butterfly"}

is an assignment statement: it assigns a literal value of the Book struct to a variable named b, which must already exist.

We can think of this as assigning labels to values; from now own, we can refer to that Book value via the name b, and everytime the name b occurs in the code, it will be translated to that Book value.

Short Variable Declaration

It’s so common in Go to want to declare a variable and then assign a value to it that there’s actually a special syntax form to do just this: the short variable declaration.

It looks like this:

1b := Book{Title: "The Making of a Butterfly"}

The type is inferred from the type of the literal.

N.B. Short variable declaration is only allowed inside functions.

This both declares a new variable b, and assigns it a Book literal, in a single statement. It is a shorthand for

1var b = Book{Title: "The Making of a Butterfly"}

or even

1var b Book = Book{Title: "The Making of a Butterfly"}
1var c float64 = 2

References

Next -> declaration-in-go

Next -> assigning-more-than-one-value-in-go

#basics #assignment #programming #golang #statement