In this tutorial, we will learn about variables in Go language. We will cover the basics of declaring and using variables, including naming conventions.
Variables in Go are used to store data values that can be manipulated during program execution. They have a type that determines the kind of data they can hold, such as integers, strings, or boolean values.
When naming variables in Go, follow these conventions:
The syntax to declare variables in Go is:
var variable_name data_type
We can store an integer value in a variable.
For example,
num
.package main
import (
"fmt"
)
func main() {
// Declare and initialize an integer variable
num := 10
// Print the value of the variable
fmt.Println("The value of num is:", num)
}
The value of num is: 10
We can store a string value in a variable.
For example,
name
.package main
import (
"fmt"
)
func main() {
// Declare and initialize a string variable
name := "John"
// Print the value of the variable
fmt.Println("The value of name is:", name)
}
The value of name is: John
We can store a boolean value in a variable.
For example,
isTrue
.true
to the variable.package main
import (
"fmt"
)
func main() {
// Declare and initialize a boolean variable
isTrue := true
// Print the value of the variable
fmt.Println("The value of isTrue is:", isTrue)
}
The value of isTrue is: true