In this lesson, we will learn:
- What is a variable?
- Two declaration and assignment methods.
- Side note : Comments
1. What is a variable?
To understand what variables are, think of this example: suppose there is a title 'Guest', every person who books a room will be a 'Guest' until they leave. Another person will book the room and take the title 'Guest'... So, the title 'Guest' is always there to be taken.
Consider our inn room as being a storage memory (i.e RAM), that is: space where you can store data. The persons that book the room as values. And the title Guest as a variable linked to the room itself, the title whose value changes: Sometimes it takes the value of 'Joe', sometimes 'Mark' or 'Jack'..etc.
To declare a variable, then, is to build the room (prepare storage memory in other words) and create the title Guest (call that memory location Guest). In technical terms, we say : to allocate memory or memory allocation.
2. Two declaration and assignment methods
In order to declare a variable in Go, we use one of two methods:
- The traditional declaration (with var).
- The short declaration
To declare a variable with var, following is the syntax:
var guest string
The previous code declares a variable with the name guest, of type string. You can think of strings as text variables, like names, addresses, messages...etc.
To declare a variable and assign a value at the same time:
var guest string = "Joe"
Here, we allocate a memory location (we will see the size of this memory for strings and other types in a future lesson), and we call it guest , and we store a value "Joe" in it, that is the name of the guest.
You can also declare the variable guest in one line, and assign the value in another line, like this :
var guest string
/**
some code here
**/
guest = "Joe"
Remember that = does not mean equals in Go ( == is used instead for equality checks). It is an assignment, that is : we assign the value "Joe" to the variable Guest, in other words, we save the string "Joe" in a memory location we already allocated by var guest string.
For the type string, there are other types for integers, floating-point numbers, booleans...etc. So, we can use var age int, var price float32 for example.
There is more to be said about declarations and assignments with var , but for the sake of simplicity, we will back to that later.
To declare a variable (and assign a value) using the short declaration method, you can use the following syntax (with the symbol := ) :
guest := "Joe"
And it says : declare a variable whose name is guest, and assign (or store) a value "Joe" to it. The Go compiler automatically knows that guest is of type string as it can guess it from the value, as "Joe" cannot be a number.
Remember : You can only use this type of declaration and assignment inside a function's body. Like this :
func newGuest(){
guest := "Joe"
}
But never outside a function.
Now as we have seen the two methods of declaring and assignment values to variables, when it is recommended to use each ?
As a general rule of thumb : use the short method with := whenever you have these conditions met (at least).
- The variable is being declared inside a function.
- The desired type is surely the type the compiler will guess.
- There source code is big and messy, and you need to get done in fewer lines
The first is obvious, since you cannot use := outside a function.
As for the second, suppose that you have a variable Latitude that you declared and initialized with a value of zero, with the intention of changing that value later to 48.864716 ,which is latitude of Paris in the Geographic Coordinate System (GCS).
latitude := 0
/** some code that retrieves
the latitude of Paris **/
latitude = 48.864716
There is an error in the previous code, the use of latitude := 0 declares a variable latitude of type int (i.e : integer), since the compiler guesses that the variable is an integer, while the value 48.864716 is a floating point. So, there is an error here.
$ constant 48.8647 truncated to integer
To fix this, either use var latitude float32 , or use latitude := 0.00 to tell the compiler that it is in fact a floating-point number variable that you want to declare :
/*Either use this:*/
latitude := 0.00
latitude = 48.864716
/*Or this:*/
var latitude float32
latitude = 48.864716
/*But this is an error!*/
latitude := 0 /*declared and assigned integer*/
latitude = 48.864716 /*assigns a float to an integer!*/
We will learn more about variables, declarations, assignments and types in future lessons.
3. Side Note : Comments
Comments are used to add notes, or to ignore some code in case you want to test something. Single line comments start with // , and multi-line comments open with /* and close with */
var guest string = "Xiao Ming"
/*
This is a multi-line
comment and will be
ignored by the compiler.
This code will be ignored too :
fmt.Println("I will not be compiled !")
*/
fmt.Printf("%s is our guest")
//this is a comment and will be ignored
That's it for this lesson, in the next lesson we will learn about constants.