Knaph

Basic Types

The basic types

The ones you'll use constantly:

var i int        // platform-sized integer (64-bit on virtually everything today)
var f float64     // 64-bit floating point
var b bool        // true or false
var s string      // UTF-8 text, immutable

There are also sized integer types (int8, int32, uint64, ...) for when the exact width matters — packing bytes for a network protocol, say — but reach for plain int by default.

A variable declared with var and no value gets its type's zero value, not null/None/undefined:

var count int     // 0
var label string  // ""
var ready bool    // false

Every type has exactly one zero value:

TypeZero value
int, int8int64, uintuint64, byte, rune0
float32, float640
complex64, complex1280+0i
boolfalse
string"" (empty, not nil)
pointer (*T)nil
slice ([]T)nil (behaves as an empty, length-0 slice)
map (map[K]V)nil (readable as empty; writing to it panics)
channel (chan T)nil
functionnil
interfacenil
structeach field set to its own zero value
array ([N]T)every element set to T's zero value

This is deliberate: Go doesn't have a universal nil-for-anything the way Python has None for every variable. Only a handful of types (pointers, slices, maps, interfaces, channels, functions) can be nil; an int or string always has a real, usable zero value, so there's no "forgot to initialize it, now it's null and everything downstream throws" class of bug for these types.

Sign in to track your progress through this course.

Ask an AI

Open a ready-made prompt in ChatGPT or Claude — just press Enter.

SummarizeChatGPTClaude
Ask me questionsChatGPTClaude
Let's learn togetherChatGPTClaude