HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Variables are containers for storing data values
In Go, there are different types of variables, for example:
int
- stores integers (whole numbers), such as 123 or -123float32
- stores floating point numbers, with decimals, such as 19.99 or -19.99string
- stores text, such as "Hello World". String values are surrounded by double quotesbool
- stores values with two states: true or falseMore about different variable types, will be explained in the Go Data Types chapter.
In Go, there are two ways to declare a variable:
1. With the var
keyword:
Use the var
keyword, followed by variable name and type:
var variablename type = value
type
or value
(or both).
2. With the :=
sign:
Use the :=
sign, followed by the variable value:
variablename := value
:=
, without assigning a value to it.
If the value of a variable is known from the start, you can declare the variable and assign a value to it on one line:
package main
import ("fmt")
func main() {
var student1 string = "John" //type is
string
var student2 = "Jane" //type is inferred
x := 2 //type is inferred
fmt.Println(student1)
fmt.Println(student2)
fmt.Println(x)
}
student2
and x
is inferred from
their values.
In Go, all variables are initialized. So, if you declare a variable without an initial value, its value will be set to the default value of its type:
package main
import ("fmt")
func main() {
var a string
var b int
var c bool
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
Example explained
In this example there are 3 variables:
a
b
c
These variables are declared but they have not been assigned initial values.
By running the code, we can see that they already have the default values of their respective types:
a
is ""
b
is 0
c
is false
It is possible to assign a value to a variable after it is declared. This is helpful for cases the value is not initially known.
package main
import ("fmt")
func main() {
var student1 string
student1 = "John"
fmt.Println(student1)
}
Note: It is not possible to declare a variable using ":=
" without assigning a value to it.
There are some small differences between the var
var :=
:
var | := |
---|---|
Can be used inside and outside of functions | Can only be used inside functions |
Variable declaration and value assignment can be done separately | Variable declaration and value assignment cannot be done separately (must be done in the same line) |
This example shows declaring variables outside of a function, with the var
keyword:
package main
import ("fmt")
var a int
var b int = 2
var c = 3
func main() {
a = 1
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
Since :=
is used outside of a function, running the program results in an error.
package main
import ("fmt")
a := 1
func main() {
fmt.Println(a)
}
Result:
./prog.go:5:1: syntax error: non-declaration statement outside function body