Lesson 2
Understanding Variables and Constants in Go
Topic Overview and Goal Setting

Greetings! As we venture into our Go Programming Expedition, we're setting out to understand Go Variables, our essential assistants. Similar to coordinates on a map, variables guide our code, endowing it with data and meaning.

In simple terms, a variable in coding is akin to a ticket — a reserved place in memory where a value can be stored. This lesson aims to demystify the concept of Go variables, examining their definition, naming conventions, assignment operations, and the concept of constant variables.

What are Go Variables?

Think of Go variables as tickets, each carrying specific data. The following short example illustrates how a variable is defined in Go:

Go
1var numOfMountainPeaks int // We declare a variable, similar to buying a ticket 2numOfMountainPeaks = 14 // We then assign it a value 3fmt.Println(numOfMountainPeaks) // Finally, we validate its contents. It outputs: 14

Here, int is the data type of the variable (integer), numOfMountainPeaks is the variable's name, and 14 is its value. We will delve deeper into data types in the upcoming lesson, so don't fret if the int part is somewhat unclear now.

Alternatively, you can declare and assign the variable in one step like this:

Go
1var numOfMountainPeaks = 14 // declaring and assigning the variable at once 2fmt.Println(numOfMountainPeaks)

Or using a short declaration:

Go
1numOfMountainPeaks := 14 // creating and assigning the variable with shorthand 2fmt.Println(numOfMountainPeaks)

To sum up, here are all the ways to initialize a variable:

  • Declaring without initializing: var name int;
  • Declaring and Initializing: var name = 5;
  • Short Declaring and Initializing: name := 5;
Go Naming Conventions

Just as with correctly labeling a ticket, naming a Go variable requires adherence to certain rules and conventions. These assist us in keeping our code error-free and easily interpreted by others.

Go's variable name rules follow the CamelCase convention: If the variable name contains a single word, all letters should be lowercase. If the variable name comprises multiple words, the first one should be lowercase, and each subsequent one should start with a capital letter. For example, age, weight, myAge, firstDayOfWeek.

Special characters and digits are not permitted at the start of variable names.

Go
1// Correct variable naming 2var myWeight int = 72 3var district9Population int = 10000 4myAge := 20 5 6// Incorrect variable naming (commented intentionally) 7// var 0zero = 0; 8// var ?questionMark = 1;
Assignment Operations in Go

Assigning in Go involves allocating or updating a variable's value using the = operator. This act is analogous to stamping a ticket.

Go
1var constellations = 88 // We secure a ticket, label it, and assign a value 2fmt.Println(constellations) // We check the content. It outputs: 88 3 4constellations = 77 // We modify the value of the variable 5fmt.Println(constellations) // We review the updated content. It outputs: 77
Go Constants

While the previous section describes how to change a variable's value, Go also provides a method to define constants — variables that cannot alter their value once assigned. We use the const keyword to declare a constant. Constants are generally named using uppercase letters, and words are separated by underscores _.

Declaring a value as const is a common practice when you know it won't change, thereby enhancing readability, providing safety (avoiding accidental changes), and sometimes improving performance reasons.

Go
1const DAYS_IN_WEEK = 7 // We define a constant, similar to etching a fact on a monument 2fmt.Println(DAYS_IN_WEEK) // We examine our immutable fact. It outputs: 7 3 4// DAYS_IN_WEEK = 6; // This will not compile

Here, DAYS_IN_WEEK serves as a constant, disallowing adjustments once assigned. The value for this variable cannot be changed after its assignment.

Lesson Recap and Next Steps

Great job! You've now gained insights into the basics of variables and constants in Go. In our upcoming lessons, we'll apply these concepts through practical exercises. Practicing is indeed crucial for transforming knowledge into skill, so let's delve into the tasks and continue our Go Programming Expedition!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.