The most general form to declare a variable in Golang uses the var keyword, an explicit type.
Examples for Variable declaration and initialization using Golang:
package main
import "fmt"
func main() {
// 1. Declare a variable and initialize it [Typed Variable]
var a string
a = "vignesh"
fmt.Println(a)
// declaration and initialization at a same time
// var a string = "vignesh"
//default values for types:
// int -> 0, string -> "",
// float -> 0, byte -> 0
// bool -> false
// 2. Shortcut variable declaration (dynamic)
b := "vicky"
fmt.Println(b)
// 3. Go can initialize the type of the variable [UnTyped Variable]
var c = false
fmt.Println(c)
// 4.We can declare and initialize multiple variables with multiple types at a time
var x, y, z = 1, "abc", 12.0
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
m, n := 1, 1.2
fmt.Println(m)
fmt.Println(n)
}
Output:
vignesh
vicky
false
1
abc
12
1
1.2
Comments
Post a Comment