Values
Go's built-in scalar types - bool, integer variants, float32/float64, byte, rune, and string.
Go is statically typed - every value has a type known at compile time. The built-in scalar types cover booleans, integers of specific widths, floating-point numbers, and text.
Boolean values are true or false. Go has no truthiness - non-zero integers and non-nil pointers are not implicitly boolean.
package main
import "fmt"
func main() {
fmt.Println(true && false) // false
fmt.Println(true || false) // true
fmt.Println(!true) // false
}Go has signed integers (int8, int16, int32, int64, and int), unsigned variants (uint8/byte, uint16, uint32, uint64, uint), and two floating-point types. int and uint are platform-width - 64 bits on modern systems.
package main
import "fmt"
func main() {
fmt.Println(1 + 2) // 3 (int)
fmt.Println(7.0 / 3.0) // 2.3333… (float64)
fmt.Println(7 / 3) // 2 (integer division)
fmt.Println(7 % 3) // 1
var b byte = 255 // alias for uint8
fmt.Println(b) // 255
}Strings in Go are immutable sequences of bytes (UTF-8 encoded). rune is an alias for int32 and represents a Unicode code point. byte is an alias for uint8 and represents a raw byte.
package main
import "fmt"
func main() {
s := "Hello, 世界"
fmt.Println(len(s)) // 13 - byte count, not character count
fmt.Println([]rune(s)) // slice of Unicode code points
for i, r := range s {
fmt.Printf("%d: %c\n", i, r)
}
// i is the byte offset; r is the rune at that offset
}In production
range over a string yields runes (Unicode code points); len() returns bytes. Mixing them causes silent truncation on emoji and CJK input - s[3] gives the fourth byte, not the fourth character. In international products, always iterate with range when you care about user-visible characters, and use utf8.RuneCountInString instead of len when you need a character count for display or truncation.
Enjoyed this? Get more essays on software craft delivered to your inbox.
Subscribe free