Types & Variables
Static and inferred, zero values, and no implicit conversion anywhere.
var
var name type = value declares a variable. The type comes after the name:
package main
import "fmt"
func main() {
var count int = 10
fmt.Println(count)
}Give it a value and the type is inferred; give it a type and you get the zero value. You rarely write all three parts:
package main
import "fmt"
func main() {
var a = 10
var b int
fmt.Println(a)
fmt.Println(b)
}Short declaration
Inside a function, := declares and assigns in one step:
package main
import "fmt"
func main() {
count := 10
name := "ada"
fmt.Println(count)
fmt.Println(name)
}It declares, so a second := on the same name is an error — use = to reassign:
package main
import "fmt"
func main() {
count := 10
count = 20
fmt.Println(count)
}const
A const is fixed at compile time. An untyped one takes the type it is used with, so
Pi * 10 works where a float64 variable times an int would not:
package main
import "fmt"
const Pi = 3.14159
func main() {
fmt.Println(Pi)
fmt.Println(Pi * 10)
}Zero values
Every type has a zero value, and a declared variable always has one:
package main
import "fmt"
func main() {
var s string
var i int
var f float64
var b bool
var p *int
fmt.Printf("%q\n", s)
fmt.Println(i)
fmt.Println(f)
fmt.Println(b)
fmt.Println(p)
}The numeric types
int is the one you reach for. The sized types exist for when the width matters:
| Type | Is |
|---|---|
int, uint |
64-bit on any modern machine |
int8 int16 int32 int64 |
exactly that many bits |
float64 |
the default float; float32 when you must |
byte |
an alias for uint8 |
rune |
an alias for int32, holding one Unicode code point |
Integer division truncates, as it does in C and Java:
package main
import "fmt"
func main() {
fmt.Println(7 / 2)
fmt.Println(7.0 / 2.0)
fmt.Println(7 % 2)
}Operators
Arithmetic, and the assignment shorthands:
package main
import "fmt"
func main() {
a, b := 7, 2
fmt.Println(a + b)
fmt.Println(a - b)
fmt.Println(a * b)
fmt.Println(a / b)
fmt.Println(a % b)
}package main
import "fmt"
func main() {
n := 5
n += 3
n *= 2
n++
fmt.Println(n)
}Comparison returns a bool, and the logical operators short-circuit:
package main
import "fmt"
func main() {
a, b := 7, 2
fmt.Println(a == b)
fmt.Println(a > b)
fmt.Println(a == b && a > b)
fmt.Println(a == b || a > b)
}The full set:
| Operators | For |
|---|---|
+ - * / % |
arithmetic; + also concatenates strings |
+= -= *= /= %= |
assignment shorthand |
++ -- |
statements only |
== != < <= > >= |
comparison, returning bool |
&& || ! |
logical, short-circuiting |
& | ^ << >> |
bitwise and shifts |
& * |
address-of and dereference (chapter 7) |
No implicit conversion
Go will not mix numeric types, even widening ones:
package main
import "fmt"
func main() {
var i int = 10
var f float64 = 2.5
fmt.Println(i * f)
}Converting
T(v) converts explicitly:
package main
import "fmt"
func main() {
var f float64 = 3.9
var i int = int(f)
var small int8 = 25
var big int64 = int64(small)
fmt.Println(i)
fmt.Println(big)
}A conversion that does not fit wraps around, silently:
package main
import "fmt"
func main() {
var big int64 = 300
fmt.Println(int8(big))
}Text is different — strconv, not a conversion. Atoi returns two values, the
number and an error, so n, _ := discards the second:
package main
import (
"fmt"
"strconv"
)
func main() {
n, _ := strconv.Atoi("42")
fmt.Println(n + 1)
s := strconv.Itoa(42)
fmt.Println(s + "!")
}Floats have their own pair, and ParseFloat takes the bit size:
package main
import (
"fmt"
"strconv"
)
func main() {
f, _ := strconv.ParseFloat("3.14", 64)
fmt.Println(f * 2)
s := strconv.FormatFloat(3.14159, 'f', 2, 64)
fmt.Println(s)
}Both return an error when the text does not parse:
package main
import (
"fmt"
"strconv"
)
func main() {
_, err := strconv.Atoi("abc")
fmt.Println(err)
}Strings and bools
A string is immutable, and len counts bytes:
package main
import "fmt"
func main() {
fmt.Println(len("hello"))
fmt.Println(len("héllo"))
}There is no truthiness. A condition must be an actual bool:
package main
import "fmt"
func main() {
var count int = 1
if count {
fmt.Println("yes")
}
}Your own types
type makes a genuinely new type, not an alias:
package main
import "fmt"
type UserID int
func main() {
var id UserID = 7
fmt.Println(id)
}The compiler then keeps it apart from the type it was built on:
package main
import "fmt"
type UserID int
func main() {
var id UserID = 7
var n int = 3
fmt.Println(id + n)
}