In this example, we will show you how to declare and use variables in Go with examples.
Variables are used to store values. They are labels given to the values. Go uses the var keyword to declare a list of variables. We can also use the := shorthand syntax to declare variables.
1. Go declare variable example
In the example, we declare and initialize two variables. Later, we print them.
package main
import "fmt"
func main() {
var i int = 10
var w float64 = 10.5
fmt.Println(i)
fmt.Println(w)
}
Output:
10 10.5
2. Go declare multiple variables example
With the var keyword, we can declare multiple variables at once.
The below example shows how to declare multiple variables with var.
package main
import "fmt"
func main() {
var i, j, k = 1, 2, 3
var (
firstName = "John"
lastName = "Cena"
)
fmt.Println(i, j, k)
fmt.Printf("%s %s\n", firstName, lastName)
}
Output:
1 2 3 John Cena
3. Go shorthand variable declaration example
Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.
The example declares two variables with the shorthand notation.
package main
import "fmt"
func main() {
name := "John"
age := 24
fmt.Println(name, age)
}
Output:
John 24
4. One more Go variable example
package main
import "fmt"
func main() {
var a = "string"
fmt.Println(a)
var b, c int = 3, 4 // Go declare multiple variables
fmt.Println(b, c)
var d = true
fmt.Println(d)
var e int
fmt.Println(e)
f := "apple" // Go shorthand variable declaration
fmt.Println(f)
}
Output:
string 3 4 true 0 apple
5. More Go Variable Examples
package main
import "fmt"
func main() {
// Step 1: Declare a variable with the 'var' keyword.
var message string
message = "Hello, Go!"
// Step 2: Initialize a variable at the time of declaration.
var greeting string = "Welcome to the world of Go!"
// Step 3: Use shorthand ':=' syntax to declare and initialize.
name := "Gopher"
// Step 4: Demonstrate variable assignment after declaration.
var age int
age = 10
// Step 5: Print the variables to output.
fmt.Println(message)
fmt.Println(greeting)
fmt.Println("Name:", name)
fmt.Printf("Age: %d\n", age)
}
Output:
Hello, Go! Welcome to the world of Go! Name: Gopher Age: 10
Explanation:
1. package main - The package declaration for the executable program.
2. import "fmt" - The import statement for the Format package, used for formatted output.
3. var message string - Declaration of a string variable message using the var keyword. It's initialized in the next line.
4. message = "Hello, Go!" - Assignment to the previously declared variable message.
5. var greeting string = "Welcome to the world of Go!" - Declaration and initialization of the greeting variable on the same line.
6. name := "Gopher" - The shorthand syntax for declaring and initializing the name variable. This syntax infers the type from the assigned value.
7. var age int and age = 10 - Declaration of an int variable age followed by assignment on the next line.
8. fmt.Println(...) and fmt.Printf(...) - Functions used to print the variables to the standard output.
9. The output displays the initialized values for each of the variables, demonstrating different ways to declare and initialize variables in Go.
Comments
Post a Comment