Are you as excited as I am to advance to the next part of our learning journey? The topic of this lesson is variables — a core concept that you will use constantly in your programming career.
In programming, a variable is akin to a container for storing information. Various types of information — such as words, numbers, and more — can be stored in this container. Here is an example of how to create a variable in Go:
Go1package main 2 3import "fmt" 4 5func main() { 6 var destination string 7 destination = "Paris" 8 fmt.Println(destination) 9} 10// Output: Paris
We've created a variable named destination
. This process is known as declaring a variable. When declaring, we choose a fitting name for the container of information, followed by the type of data we expect to store. In Go, we declare a variable using the var
keyword, followed by the variable name and finally its type. Following the declaration, we stored the string "Paris"
in it. We assign a value to a variable with the assignment operator =
. The variable that should store the value is on the left, while to acutal value is to the right.
This way of declaring a variable first and then assigning the value can get cumbersome after a while. Alternatively, we can use the shorthand operator :=
to both declare and initialize the variable, which allows Go to infer the type:
Go1package main 2 3import "fmt" 4 5func main() { 6 destination := "Paris" 7 fmt.Println(destination) 8} 9// Output: Paris
Using this syntax, we forgo both the var
keyword as well as the type declaration. The Go compiler is smart enough to know that if you are assigning a value within double quotes to a variable, the type should be a string. In Go, the term string
refers to a sequence of characters. Any text no matter the lenght is considered a string as long as it is surrounded by double quotes.
Be careful! This shorthand only works within functions (in our example, we are writing code in the main
function). If you are declaring variables out of a function, you need to use the full syntax, with the caveat that the value can be assigned at declaration time:
Go1package main 2 3import "fmt" 4 5var destination string = "Paris" 6 7func main() { 8 fmt.Println(destination) 9} 10// Output: Paris
For now we will only use variables within functions, but it is good to keep this detail in mind if you get stuck declaring variables outside of functions.
Variables play a fundamental role in most programming languages, including Go. They help reduce complexity and make our programs more readable and maintainable. For instance, if we aim to fly to various locations instead of just Paris, we can simply change the value of the variable destination
without having to modify our entire code.
In Go, variables always have a type, meaning that once your variable is declared, no other type of data can be assigned to it. This feature ensures type safety and can help prevent errors.
So, why are variables significant? They are among the fundamental building blocks of coding. Mastering them will put you on the right path to becoming a proficient programmer. Are you excited? Let's delve into the practice session and explore the world of Go variables together!