GGistDev

Values in Go

Go has simple, predictable values: booleans, numbers, strings, and composite literals. Zero values are well-defined and safe defaults.

Booleans

Booleans are either true or false. Conditions require boolean expressions; there is no implicit truthiness from ints or pointers.

b1 := true
b2 := (3 > 2)
_ = b1; _ = b2

Numbers

Go has signed and unsigned integers of various sizes, IEEE 754 floating points, and built-in complex numbers. Numeric literals support bases and separators for readability.

// integers
var i int = -42
u := uint32(7)

// floats and complex
f := 3.14
c := complex(1, -2) // 1-2i

// bases and separators
bin := 0b1010   // 10
hex := 0xFF     // 255
big := 1_000_000
_ = i; _ = u; _ = f; _ = c; _ = bin; _ = hex; _ = big

Notes:

  • int size is platform-dependent (32/64); use fixed sizes for serialization (e.g., int32, uint64)
  • complex64 and complex128 pair float components

Strings

UTF-8 strings are immutable byte sequences. Interpreted strings use escapes; raw strings use backticks and do not process escapes.

s := "hello\nworld"     // interpreted
raw := `path\to\file`  // raw string literal (no escapes)

// concatenation
msg := "Hello, " + "Go"
_ = s; _ = raw; _ = msg

Runes and bytes

rune is an alias for int32 and represents a Unicode code point; byte is an alias for uint8.

var r rune = 'é'    // Unicode code point
var b byte = 'A'    // alias for uint8
_ = r; _ = b

Notes:

  • A rune is an alias for int32 representing a Unicode code point
  • Indexing a string yields bytes; use for range to iterate runes

Composite literals (overview)

Composite literals construct arrays, slices, maps, and structs inline. Slices and maps are references to underlying storage.

arr := [3]int{1,2,3}
slc := []int{1,2,3}
mp  := map[string]int{"a":1}
str := struct{X int}{X: 10}
_ = arr; _ = slc; _ = mp; _ = str

Slices and maps are reference types; zero value is nil. Allocate before writing.

Zero values

Every type has a zero value: numeric types zero to 0, booleans to false, strings to empty string, reference types to nil.

var (
    zi int        // 0
    zs string     // ""
    zb bool       // false
    zsli []int    // nil
    zm map[int]int // nil
)

Summary

  • Literals are clear and explicit; underscores improve readability
  • Zero values are safe defaults; initialize reference types (maps, slices) before writing
  • Prefer fixed-size ints for I/O formats; use runes for Unicode-aware processing