Go Variables
A variable is a named place in your program where a value is kept, so you can use it again later.
Go Variable Types
A variable's type decides what kind of data it can hold. Go has several types, including these:
int- whole numbers (integers), like 123 or -123float32- floating point numbers, which have decimals, like 19.99 or -19.99string- text, like "Hello World", written inside double quotesbool- one of two states, true or false
Declaring (Creating) Variables
Go gives you two ways to declare a variable. The syntax for each one is shown below.
1. The var keyword:
Write var, then the name of the variable and its type.
Note: You can leave out the type or the value, but never both.
2. The := sign:
Write the variable name, then :=, then the value.
Note: With this form the type is inferred: the compiler works it out from the value you provide.
Note: Because the compiler needs that value, := can't be used to declare a variable without assigning one.
Var Syntax syntax
var variablename type = value
Short Syntax syntax
variablename := value
Variable Declaration With Initial Value
When you already know what a variable should hold, you can do the declaration and the assignment in a single line, as the example below shows.
Note: Neither student2 nor x has a type written in the code, so their types are inferred from their values.
Example
Output: Go program and captured output
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)
}
Variable Declaration Without Initial Value
Go never leaves a variable uninitialized. Declare one without giving it a value, and it automatically receives the default value for its type:
Example
The program below declares three variables, a, b and c, and gives none of them a value. Running it shows that each one already holds the default for its type: a is "", b is 0 and c is false.
Output: Go program and captured output
package main
import ("fmt")
func main() {
var a string
var b int
var c bool
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
Value Assignment After Declaration
You don't have to give a variable its value straight away. You can declare it first and assign the value later, which is useful when you don't know the value yet.
Note: This only works with var. Using ":=" always requires a value as part of the declaration.
Example
Output: Go program and captured output
package main
import ("fmt")
func main() {
var student1 string
student1 = "John"
fmt.Println(student1)
}
Difference Between var and :=
var and := differ in a couple of small ways:
You can use var both inside and outside functions, and you can declare the variable and assign its value in separate steps.
:= works only inside functions, and the declaration and assignment cannot be separated: they have to happen on one line.
Example
Here, var declares variables outside any function, which works without a problem:
Output: Go program and captured output
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)
}
Example
This program tries to use := outside a function, so it fails with an error instead of running.
Output: Go program and captured output
package main
import ("fmt")
a := 1
func main() {
fmt.Println(a)
}