Variables are names that hold data which can change while the program is running. They let you store and use information in your program. For Example:
package main
import "fmt"
func main() {
var number int = 10
fmt.Println("The number is:", number)
}
Output
The number is: 10
Explanation:
- var number int = 10: creates a variable called number to store the value 10.
- fmt.Println: shows the value stored in the variable.
Rules for Naming Variables
- Variable names must start with a letter or underscore (_).
- Names can contain letters (a-z, A-Z), digits (0-9), and underscores _.
- Variable names cannot start with a digit.
- Variable names are case-sensitive: geeks and Geeks are different.
- Keywords cannot be used as variable names.
- No strict limit on length, but 4–15 characters is recommended.
Valid examples:
Geeks, geeks, _geeks23
Invalid examples:
123Geeks, 23geeks
Declaring Variables in Go
In Go language variables are created in two different ways:
- Using the var keyword
- Using short variable declaration (:=)
Using var keyword
Variables can be declared using the var keyword by specifying the variable name, type, and initial value.
Syntax
var variable_name type = expression
- In the above syntax, either type or = expression can be omitted, but not both.
- If the = expression is omitted, the variable is assigned the zero value of its type: 0 for numbers, false for Booleans, "" for strings, and nil for pointers or reference types.
- If the type is removed, then the type of the variable is determined by the value-initialize in the expression.
Example: Variable Declaration with Type Inference
package main
import "fmt"
func main() {
var myvariable1 = 20
var myvariable2 = "GeeksforGeeks"
var myvariable3 = 34.80
fmt.Printf("The value of myvariable1 is : %d\n", myvariable1)
fmt.Printf("The type of myvariable1 is : %T\n", myvariable1)
fmt.Printf("The value of myvariable2 is : %s\n", myvariable2)
fmt.Printf("The type of myvariable2 is : %T\n", myvariable2)
fmt.Printf("The value of myvariable3 is : %f\n", myvariable3)
fmt.Printf("The type of myvariable3 is : %T\n", myvariable3)
}
Output
The value of myvariable1 is : 20 The type of myvariable1 is : int The value of myvariable2 is : GeeksforGeeks The type of myvariable2 is : string The value of myvariable3 is : 34.800000 The type of my...
Zero-Value Variables
If the expression is removed, the variable holds the zero-value of its type:
- 0 for numeric types
- false for boolean
- "" for strings
- nil for reference and interface types
Example:
package main
import "fmt"
func main() {
var myvariable1 int
var myvariable2 string
var myvariable3 float64
fmt.Printf("myvariable1: %d\n", myvariable1)
fmt.Printf("myvariable2: %s\n", myvariable2)
fmt.Printf("myvariable3: %f\n", myvariable3)
}
Output
myvariable1: 0 myvariable2: myvariable3: 0.000000
Declaring Multiple Variables of the Same Type
In Go, you can declare and initialize several variables of the same type in one line, which makes the code shorter and easier to read.
var a, b, c int = 2, 45, 67
Output
a: 2, b: 45, c: 67
Declaring Multiple Variables of Different Types
Multiple variables of different types can be declared in a single line. If the type is not specified, Go automatically assigns a type based on the value.
var a, b, c = 2, "GFG", 67.56
Output
a: 2 (int), b: GFG (string), c: 67.560000 (float64)
Multiple Return Values from a Function
Variables can be initialized using a function that returns more than one value, allowing you to handle multiple results at once.
package main
import "fmt"
func getNumbers() (int, int) {
return 5, 10
}
func main() {
a, b := getNumbers()
fmt.Println("First number:", a)
fmt.Println("Second number:", b)
}
Output
First number: 5 Second number: 10
Explanation:
- getNumbers() returns two numbers.
- a, b := getNumbers() stores both numbers in two variables at once.
Using Short Variable Declaration
The local variables which are declared and initialize in the functions are declared by using short variable declaration.
Syntax
variable_name := expression
Note: Please don't confuse in between := and = as := is a declaration and = is assignment.
Important Points: In the above expression, the type of the variable is determined by the type of the expression.
package main
import "fmt"
func main() {
myvar1 := 39
myvar2 := "GeeksforGeeks"
myvar3 := 34.67
fmt.Printf("myvar1: %d (%T)\n", myvar1, myvar1)
fmt.Printf("myvar2: %s (%T)\n", myvar2, myvar2)
fmt.Printf("myvar3: %f (%T)\n", myvar3, myvar3)
}
Output
myvar1: 39 (int) myvar2: GeeksforGeeks (string) myvar3: 34.670000 (float64)
Declaring Multiple Variables Using := (Same Type)
Multiple local variables of the same type can be declared and initialized in a single line using the short variable declaration :=
myvar1, myvar2, myvar3 := 800, 34, 56
Output
myvar1: 800 (int)
myvar2: 34 (int)
myvar3: 56 (int)
Declaring Multiple Variables Using := (Different Type)
Multiple local variables of different types can be declared and initialized in a single line using the short variable declaration :=, and Go automatically determines their types from the values.
myvar1, myvar2, myvar3 := 800, "Geeks", 47.56
Output
myvar1: 800 (int)
myvar2: Geeks (string)
myvar3: 47.560000 (float64)
Multiple Return Values with Short Declaration
Variables can be initialized using a function that returns multiple values, and the short declaration := can be used to store them in a single line.
package main
import "fmt"
func getValues() (int, string) {
return 7, "GoLang"
}
func main() {
x, y := getValues()
fmt.Println("Number:", x)
fmt.Println("Text:", y)
}
Output
Number: 7 Text: GoLang
Explanation:
- getValues() returns two values: an int and a string.
- x, y := getValues() uses short variable declaration := to assign both values at once.
Short Declaration as Assignment
A short variable declaration works like an assignment only if at least one variable is new in the same block.
package main
import "fmt"
func main() {
myvar1, myvar2 := 39, 45
myvar3, myvar2 := 45, 100
fmt.Printf("The value of myvar1 and myvar2 is : %d %d\n", myvar1, myvar2)
fmt.Printf("The value of myvar3 and myvar2 is : %d %d\n", myvar3, myvar2)
}
Output
The value of myvar1 and myvar2 is : 39 100 The value of myvar3 and myvar2 is : 45 100