Slide 1

Slide 1 text

Day #2 - Constants and Variables Constant and variable declarations in Go 「 Trying to make you to fall in love with Golang 」 1 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 2

Slide 2 text

Constant Declarations Constants are declared by using the const keyword - Try it! the = symbol means "bind" in the example on the right, constants π and Pi are both bound to the literal 3.1416 constants can be declared both at package level (out of any function body) and in function bodies (local constants) 2 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 3

Slide 3 text

Typed Constants Constants are typed when you explicitly specify the type in the declaration like this: if multiple typed constants are declared on the same line, then their types must be the same as the constants A and B in the above example 「Trying to make you to fall in love with Golang」 - Constants and Variables 3 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 4

Slide 4 text

Autocomplete in constant declaration A constant declaration is incomplete when it doesn't contains the = symbol. In this cas the compiler will autocomplete the incomplete lines for us by copying the missing part from the first preceding complete declaration. Try It! _ is a special (blank) identifier - you can read it as "skip" 4 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 5

Slide 5 text

iota constant generator feature iota is a built-in constant which can only be used in other constant declarations declared as const iota = 0 Helpful when we want to autogenerate constant values. Try It! 5 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 6

Slide 6 text

Variables The value represented by a variable can be modified at run time. when declared within function bodies are called local variables when declared out of any function body are called package-level variables There are two variable declaration forms the standard one var lang string = "Go" the short one lang := "Go" - can only be used to declare local variables 「Trying to make you to fall in love with Golang」 - Constants and Variables 6 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 7

Slide 7 text

Standard variable declaration forms Starts with the var keyword, then is followed by the name, eventually the type, the assignment operator (=) and the initial value. In pratice the type isn't specified since the compiler can deduce it by the initial value. 「Trying to make you to fall in love with Golang」 - Constants and Variables 7 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 8

Slide 8 text

Short variable declaration forms the keyword var and the type must be omitted the assignment sign must be := instead of = Try It! 8 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 9

Slide 9 text

Each local declared variable must be used Go compiler don't allow unused local declared variables. package-level variables have no such limit Try It! 9 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 10

Slide 10 text

Thank you Luca Sepe Solution Architect (@day) / Software Craftsman (@night) {twitter.com, github.com, linkedin.com} / lucasepe 「Trying to make you to fall in love with Golang」 - Constants and Variables 10 / 10 Constants and Variables Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/