Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Constant and variable declarations in Go

Constant and variable declarations in Go

Luca Sepe

May 20, 2020
Tweet

More Decks by Luca Sepe

Other Decks in Programming

Transcript

  1. 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/
  2. 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/
  3. 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/
  4. 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/
  5. 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/
  6. 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/
  7. 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/
  8. 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/
  9. 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/
  10. 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/