Back to Gotraining

README

topics/go/language/variables/README.md

latest2.1 KB
Original Source

Variables

Variables are at the heart of the language and provide the ability to read from and write to memory. In Go, access to memory is type safe. This means the compiler takes type seriously and will not allow us to use variables outside the scope of how they are declared.

Notes

  • The purpose of all programs and all parts of those programs is to transform data from one form to the other.
  • Code primarily allocates, reads and writes to memory.
  • Understanding type is crucial to writing good code and understanding code.
  • If you don't understand the data, you don't understand the problem.
  • You understand the problem better by understanding the data.
  • When variables are being declared to their zero value, use the keyword var.
  • When variables are being declared and initialized, use the short variable declaration operator.

Built-In Types
Variables
Gustavo's IEEE-754 Brain Teaser - William Kennedy
What's in a name
A brief history of “type” - Arcane Sentiment

Code Review

Declare and initialize variables (Go Playground)

Exercises

Exercise 1

Part A: Declare three variables that are initialized to their zero value and three declared with a literal value. Declare variables of type string, int and bool. Display the values of those variables.

Part B: Declare a new variable of type float32 and initialize the variable by converting the literal value of Pi (3.14).

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.