Skip to Content
Go Realm v1 is released 🎉
TopicsData Types

Data Types in Go

Go is statically typed — every variable has a type known at compile time. No implicit type coercion between different types.


1. Type Categories

CategoryTypesBehaviour
Basicbool, int*, uint*, float*, complex*, string, byte, runeValue type — copied on assignment
Compositearray, structValue type — fully copied
Referenceslice, map, channel, pointer, funcHeader copied, underlying data shared
Interfaceinterface{} / anyHolds type + value pair

2. Numeric Types

TypeSize (bits)RangeUse when
int32 or 64platform-dependentgeneral integers — default choice
int88-128 to 127tight memory constraints
int1616-32 768 to 32 767protocol fields
int32 / rune32-2³¹ to 2³¹-1Unicode code point
int6464-2⁶³ to 2⁶³-1timestamps, large IDs
uint8 / byte80 to 255raw bytes, image pixels
uint16160 to 65 535ports, small counts
uint32320 to 2³²-1IPv4, file sizes
uint64640 to 2⁶⁴-1large counters
float3232±1.18e-38 to ±3.4e38graphics, sensors
float6464±2.23e-308 to ±1.8e308default float — always prefer
complex6464float32 partsscientific computing
complex128128float64 partsscientific computing

Rule: Use int for integers, float64 for floats, unless you have a specific reason to use a smaller type.


3. Other Basic Types

var b bool = true // false by default var s string = "Hello, Go" // "" by default, immutable var r rune = 'A' // int32 alias — Unicode code point var by byte = 255 // uint8 alias — raw byte

4. Zero Values ⭐

Go always initialises variables — there is no garbage/undefined value.

TypeZero Value
int, int8int640
uint, byteuint640
float32, float640.0
boolfalse
string"" (empty, not nil)
pointer, slice, map, channel, funcnil
structall fields set to their zero values
arrayall elements set to their zero values
var i int // 0 var f float64 // 0.0 var s string // "" var b bool // false var p *int // nil

5. Variable Declaration

// Full form — explicit type var name string = "Go" // Type inferred from value var name = "Go" // Short declaration — inside functions only name := "Go" // Multiple variables var x, y int = 1, 2 a, b := 10, "hello" // Block declaration var ( host string = "localhost" port int = 8080 tls bool = true )

6. Type Conversion

Go has no implicit conversion — all conversions are explicit.

var i int = 42 var f float64 = float64(i) // must be explicit var u uint = uint(f) // string ↔ int — NOT with a cast, use strconv s := strconv.Itoa(42) // int → string "42" n, err := strconv.Atoi("42") // string → int 42 // string(65) converts a rune, NOT an integer to its digits fmt.Println(string(65)) // "A" ← common mistake! fmt.Println(strconv.Itoa(65)) // "65" ← correct

⚠️ Interview trap: string(65) produces "A" (rune 65 = ‘A’), not "65". Use strconv.Itoa to get digits.


7. fmt Formatting Verbs

General

VerbDescriptionExample output
%vDefault format{John 25}
%+vStruct with field names{Name:John Age:25}
%#vGo syntax representationmain.Person{Name:"John"}
%TType of the valuestring, int, []int
%%Literal % sign%

Integers

VerbFormat42
%dDecimal42
%bBinary101010
%oOctal52
%xHex lowercase2a
%XHex uppercase2A

Floats

VerbFormat3.14159
%fDecimal point3.141590
%.2fDecimal, 2 places3.14
%eScientific (e)3.141593e+00
%gCompact (e or f)3.14159

Strings & Runes

VerbFormatExample output
%sPlain stringhello
%qQuoted string"hello"
%cUnicode characterA (from 65)
%UUnicode code pointU+0041
%pPointer address0xc000010050
n := 42 fmt.Printf("%d %b %o %x %X\n", n, n, n, n, n) // 42 101010 52 2a 2A pi := math.Pi fmt.Printf("%f %.2f %e %g\n", pi, pi, pi, pi) // 3.141593 3.14 3.141593e+00 3.141592653589793 s, r := "hello", 'A' fmt.Printf("%s %q %c %U\n", s, s, r, r) // hello "hello" A U+0041 type Person struct{ Name string; Age int } p := Person{"John", 25} fmt.Printf("%v\n%+v\n%#v\n", p, p, p) // {John 25} // {Name:John Age:25} // main.Person{Name:"John", Age:25}

8. Common Mistakes ⚠️

MistakeProblemFix
string(65) expecting "65"produces "A" (rune conversion)use strconv.Itoa(65)
int size assumptionint is 32-bit on 32-bit OSuse int64 when size matters
Implicit conversionfloat64(x) + int(y) won’t compilealways convert explicitly
float64 for money/financeprecision errorsuse int cents or decimal library
Comparing float64 with ==precision issuesuse math.Abs(a-b) < epsilon
Forgetting zero valuesassuming uninitialized = garbageGo zeroes everything — use this intentionally

9. Interview Cheat Sheet

Q: What is Go’s default integer type?

int — its size is platform-dependent (32 or 64 bits). For most code, use int. Use int64 when you need guaranteed 64-bit width.

Q: What is the zero value of each type?

Numerics → 0, boolfalse, string"", pointers/slices/maps/channels → nil. Go always initialises — no garbage values.

Q: What is the difference between byte and rune?

byte = uint8 (raw byte, 0–255). rune = int32 (Unicode code point). Use byte for binary data, rune for characters.

Q: Does Go do implicit type conversion?

No. All conversions must be explicit: float64(i), int(f). Mixing types without conversion is a compile error.

Q: Why use float64 over float32?

float64 is the default for all floating-point literals and math functions. float32 has less precision and rarely saves meaningful memory in practice.