In Go, variables can be defined explicitly using “var” or implicitly using “:=”. Constants are defined using “const”. In this section, we will review the various definition methods with simple code. We will also check the default values when variables are defined explicitly.
TOC
Variable
Explicit definition using “var”
When using var to define a variable, the type must be specified. var allows you to define several variables of different types together by enclosing them in parentheses ()
.
package main
import (
"fmt"
)
type SampleStruct1 struct {
ID int
Name string
}
func main() {
var (
i int
pi *int
u uint
f32 float32
f64 float64
s string
t bool
b byte
r rune
array [3]int
slice []int
ss SampleStruct1
m map[string]int
ifa interface{}
)
fmt.Printf("i: %v\t%T\n", i, i)
fmt.Printf("pi: %v\t%T\n", pi, pi)
fmt.Printf("u: %v\t%T\n", u, u)
fmt.Printf("f32: %v\t%T\n", f32, f32)
fmt.Printf("f64: %v\t%T\n", f64, f64)
fmt.Printf("s: %v\t%T\n", s, s)
fmt.Printf("t: %v\t%T\n", t, t)
fmt.Printf("b: %v\t%T\n", b, b)
fmt.Printf("r: %v\t%T\n", r, r)
fmt.Printf("array: %v\t%T\n", array, array)
fmt.Printf("slice: %v\t%T\n", slice, slice)
fmt.Printf("ss: %v\t%T\n", ss, ss)
fmt.Printf("m: %v\t%T\n", m, m)
fmt.Printf("ifa: %v\t%T\n", ifa, ifa)
}
i: 0 int
pi: <nil> *int
u: 0 uint
f32: 0 float32
f64: 0 float64
s: string
t: false bool
b: 0 uint8
r: 0 int32
array: [0 0 0] [3]int
slice: [] []int
ss: {0 } main.SampleStruct1
m: map[] map[string]int
ifa: <nil> <nil>
The default value is 0
for numeric systems, empty
for strings, and nil
for pointers.
Implicit definition using “:=”
When a value is assigned using :=
, the type definition is implicitly defined based on the assigned value.
package main
import (
"fmt"
)
type SampleStruct2 struct {
ID int
Name string
}
func main() {
i := 1
pi := &i
f := 3.14
s := "hello"
t := true
array := [3]int{100, 200, 300}
slice := []int{100, 200, 300}
ss := SampleStruct2{10, "yamada"}
m := map[string]int{"aa": 111, "bb": 222}
fmt.Printf("i: %v\t%T\n", i, i)
fmt.Printf("pi: %v\t%T\n", pi, pi)
fmt.Printf("f: %v\t%T\n", f, f)
fmt.Printf("s: %v\t%T\n", s, s)
fmt.Printf("t: %v\t%T\n", t, t)
fmt.Printf("array: %v\t%T\n", array, array)
fmt.Printf("slice: %v\t%T\n", slice, slice)
fmt.Printf("ss: %v\t%T\n", ss, ss)
fmt.Printf("m: %v\t%T\n", m, m)
}
i: 1 int
pi: 0xc0000160b0 *int
f: 3.14 float64
s: hello string
t: true bool
array: [100 200 300] [3]int
slice: [100 200 300] []int
ss: {10 yamada} main.SampleStruct2
m: map[aa:111 bb:222] map[string]int
Constant ( const )
Constants can be defined with const
. You can use iota
to generate sequential numbers.
package main
import "fmt"
type ByteSize float64
const (
Zero = iota
One
Two
)
const (
_ = iota
KB ByteSize = 1 << (10 * iota)
MB
GB
)
func main() {
fmt.Printf("Zero: %v\t%T\n", Zero, Zero)
fmt.Printf("One: %v\t%T\n", One, One)
fmt.Printf("Two: %v\t%T\n", Two, Two)
fmt.Printf("KB: %v\t%T\n", KB, KB)
fmt.Printf("MB: %v\t%T\n", MB, MB)
fmt.Printf("GB: %v\t%T\n", GB, GB)
}
Zero: 0 int
One: 1 int
Two: 2 int
KB: 1024 main.ByteSize
MB: 1.048576e+06 main.ByteSize
GB: 1.073741824e+09 main.ByteSize