These are the notes for Go episode 4. You will learn what a type really is (a box of a fixed size), what bits and bytes are, all of Go's integer types and their limits, why a counter can suddenly go negative (integer overflow), the unsigned types, float32 and float64 and why 0.1 + 0.2 is not exactly 0.3, complex numbers, bool, string, byte and rune, and Go's strictest rule: it never converts one type into another for you. Every example runs as-is in the SyntaxPilot compiler (language: Go); outputs and errors are from real runs (Go 1.24).
The problem: the college fest counter goes negative
A college fest. At the gate, a small program counts visitors: every person who walks in, plus one. 126, 127, the hall is filling up. One more person walks in, and the counter says minus 128. A negative crowd.
package main
import "fmt"
func main() {
var visitors int8 = 126
visitors++
fmt.Println(visitors)
visitors++
fmt.Println(visitors)
}
127
-128
int8is one of Go's integer types: a whole number stored in 8 bits (you will see what that means in a moment).visitors++adds one tovisitors. It is the increment statement; episode 6 (operators) covers it properly.
127, then -128. No error, no warning, no crash. Just a wrong number. Why? Because an int8 is a box of a fixed size, and 128 does not fit in it. To see why, we need to look inside the box.
Why: bits, bytes and a fixed-size box
| In a room | In memory |
|---|---|
| One light switch: on or off | a bit (short for binary digit): a 1 or a 0 |
| A board of 8 switches: 256 on/off patterns | a byte: 8 bits, so 2⁸ = 256 different values |
| A car's odometer: 999999 + 1 = 000000 | overflow: going past the top wraps around to the bottom |
Computer memory is made of tiny switches. Each one is either on or off, a one or a zero: that is a bit. Group eight bits together and you get a byte. Eight switches can make 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 256 different on-off patterns, so one byte can hold 256 different values.
Binary is the way of writing numbers with only the digits 0 and 1, which is how bits store them. Each place is worth double the place to its right: 1, 2, 4, 8, 16 and so on.
An int8 is one byte that holds a signed number. Signed means it can be negative (it has a + or - sign). Half of the 256 patterns are used for negatives, so an int8 holds -128 up to 127.
The actual bits
0111 1111 = 127 (the biggest)
1000 0000 = -128 (one more: wraps)
- The first (leftmost) bit is the sign bit: 0 means positive, 1 means negative.
- 127 is a zero followed by seven ones. Add one, and the ones carry over, exactly like 0999 + 1 = 1000 on a counter. Now the sign bit is 1, and that pattern means -128.
- So: 8 bits, 256 patterns, -128 to 127. Go past the top and you wrap around to the bottom, silently. The value didn't vanish; the box was simply too small.
This is called integer overflow (or wraparound). In Go, overflow while the program runs is silent: there is no panic (a crash; you will meet panics in episode 6) and no error message. You just get a wrong number.
The fix: a bigger box (int16, int32, int64)
Go gives you four sizes of signed integer, named by their number of bits. Each extra bit doubles how far you can count.
| Type | Size | Range (about) |
|---|---|---|
int8 |
1 byte (8 bits) | -128 to 127 |
int16 |
2 bytes (16 bits) | about ±32 thousand |
int32 |
4 bytes (32 bits) | about ±2.1 billion |
int64 |
8 bytes (64 bits) | about ±9.2 quintillion (9.2 followed by 18 digits) |
Don't trust a table, ask Go. The math package (a package is a bundle of ready-made code you bring in with import) has a constant for each limit: math.MinInt8, math.MaxInt8 and so on. (A constant is a named value that can never change; episode 5 is all about them.)
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("int8: ", math.MinInt8, "to", math.MaxInt8)
fmt.Println("int16:", math.MinInt16, "to", math.MaxInt16)
fmt.Println("int32:", math.MinInt32, "to", math.MaxInt32)
fmt.Println("int64:", math.MinInt64, "to", math.MaxInt64)
}
int8: -128 to 127
int16: -32768 to 32767
int32: -2147483648 to 2147483647
int64: -9223372036854775808 to 9223372036854775807
Notice the pattern: each range has one more negative number than positive, because zero takes one of the "positive" patterns.
This really happened: the video that broke YouTube's counter
On 4 December 2014, CBC News reported that the music video Gangnam Style had passed 2,147,483,647 views. That number is the biggest value a signed 32-bit integer can hold, and it was the limit of YouTube's view counter. Google never expected a video to be watched more times than that. Their fix was our fix: they upgraded the counter to a 64-bit integer.
Let's replay it: an int32 at its maximum gets one more view, then the same with an int64.
package main
import "fmt"
func main() {
var views int32 = 2147483647
views++
fmt.Println(views)
var fixed int64 = 2147483647
fixed++
fmt.Println(fixed)
}
-2147483648
2147483648
The int32 wrapped to minus 2.1 billion. The int64 simply counted on. Same ++, different box size.
Too big from the start: caught while compiling
What if the number is too big right from the beginning? An int8 that starts at 128:
package main
import "fmt"
func main() {
var visitors int8 = 128
fmt.Println(visitors)
}
./main.go:6:25: cannot use 128 (untyped int constant) as int8 value in variable declaration (overflows)
- Compiling is the step where Go checks your program and turns it into something the computer can run. A compile error stops the program before it ever starts.
128written directly in the code is an untyped int constant: a fixed number Go can see while compiling (episode 5 explains "untyped").- The rule: when Go can see the number while compiling, it refuses a value that doesn't fit. It only wraps silently when the value changes while the program runs (like
visitors++).
int, uint and uintptr: which integer should I use?
| Type | What it is |
|---|---|
int |
the default. 64 bits on 64-bit computers, 32 bits on 32-bit ones |
uint8, uint16, uint32, uint64, uint |
unsigned: no sign bit, no negatives |
uint8 |
0 to 255 |
uintptr |
an unsigned integer big enough to hold a memory address (low-level code) |
- The size of plain
int(anduint) is platform-dependent: it depends on the kind of processor the program runs on. 64 bits on a 64-bit computer, 32 bits on an older 32-bit one. - Unsigned means no sign bit and no negatives, so all 256 patterns of a
uint8are used for 0 up to 255. Theuintfamily isuint8,uint16,uint32,uint64and plainuint(same size asint). uintptrholds a memory address: the location of something in memory, like a house number. You'll only meet it in low-level code.- Rule: use
int, unless you have a reason to need a specific size, like a file format or a network message.
How big is int on this machine? unsafe.Sizeof (from the unsafe package) tells you how many bytes a value takes. math.MaxInt is the biggest value an int can hold.
package main
import (
"fmt"
"math"
"unsafe"
)
func main() {
var n int
fmt.Println(unsafe.Sizeof(n), "bytes")
fmt.Println(math.MaxInt)
}
8 bytes
9223372036854775807
Eight bytes, so 64 bits, and the maximum is the same as int64's. The compiler runs on a 64-bit machine.
Unsigned wraps below zero
A shop's stock is a uint8 at 0, and someone takes one more out. stock-- is the decrement statement: it subtracts one.
package main
import "fmt"
func main() {
var stock uint8 = 0
stock--
fmt.Println(stock)
}
255
Below zero, an unsigned number wraps to its top. So unsigned does not protect you from negatives, it hides them. (Writing var stock uint8 = -1 directly is caught while compiling: cannot use -1 (untyped int constant) as uint8 value in variable declaration (overflows).)
Floats: decimals on a ruler
| A school ruler | In Go |
|---|---|
| Marks every millimetre; 1/3 cm falls between marks | float64: a decimal number, about 15-16 correct digits |
| A cheaper ruler, marks only every centimetre | float32: about 7 correct digits |
A school ruler has a mark every millimetre, so a third of a centimetre falls between two marks. You get very close, but not exactly. Go's decimal types work the same way. Float is short for floating point, the way the computer stores a number with a decimal point (the point can "float" to any position). float64 uses 64 bits and has very fine marks; float32 uses 32 bits and is the cheaper ruler.
package main
import "fmt"
func main() {
var a float64 = 0.1
var b float64 = 0.2
fmt.Println(a + b)
var p float32 = 16777216
fmt.Println(p + 1)
var q float64 = 16777216
fmt.Println(q + 1)
}
0.30000000000000004
1.6777216e+07
1.6777217e+07
0.30000000000000004, not0.3. Like a third on a ruler, 0.1 has no exact mark in binary, so you get the nearest one. This is a rounding error.- The
float32plus one is still 16777216: it ran out of digits, so the+1disappeared. Thefloat64got it right. e+07is scientific notation: "times ten to the power seven".1.6777216e+07= 1.6777216 × 10,000,000 = 16,777,216.
Two lessons:
- Use
float64by default. - Never store money in a float. Store paise (whole numbers) in an
intinstead: Rs 12.50 becomes1250.
complex numbers and bool
Complex numbers, from maths class, have a real part and an imaginary part, written with an i. Go has complex64 (two float32 parts) and complex128 (two float64 parts) built in. complex(3, 4) makes 3 + 4i; real(z) and imag(z) take the parts out. A bool holds only true or false.
package main
import "fmt"
func main() {
var z complex128 = complex(3, 4)
fmt.Println(z, real(z), imag(z))
w := 2 + 3i
fmt.Printf("%T\n", w)
var ok bool = 5 > 3
fmt.Println(ok)
}
(3+4i) 3 4
complex128
true
2 + 3i is a complex128 by default. You'll rarely need complex numbers, but engineers and scientists do. 5 > 3 is a comparison: it asks a question and gives a bool, here true. (%T prints a value's type, from episode 3.)
In some languages, 1 means true. Not in Go:
package main
import "fmt"
func main() {
var ok bool = 1
fmt.Println(ok)
}
./main.go:6:19: cannot use 1 (untyped int constant) as bool value in variable declaration
A bool holds only true or false, never a number.
string, byte and rune: letters and their numbers
| A typewriter | In Go |
|---|---|
| Every key has a number: A is key 65 | byte = uint8: one byte, e.g. 'A' = 65 |
| A huge key list for every script: ₹, अ, 😀 ... | rune = int32: one Unicode character |
| A typed word: a row of keys | string: text, stored as bytes |
- Computers store letters as numbers too. A
byteis exactly one such small number;byteis another name (an alias) foruint8. - The world has far more characters than 256: the rupee sign, Hindi letters, emoji. Unicode is the giant list that gives every character in every script its own number (its code point). A
runeholds one such number;runeis another name forint32. - A
stringis text, stored inside as a row of bytes. Strings get a whole episode later. - Single quotes
'A'mean one character (a rune or byte). Double quotes"Pune"mean a string.
package main
import "fmt"
func main() {
var letter byte = 'A'
var symbol rune = '₹'
fmt.Println(letter, symbol)
fmt.Printf("%c %c\n", letter, symbol)
fmt.Printf("%T %T\n", letter, symbol)
city := "Pune"
fmt.Println(len(city), city[0])
}
65 8377
A ₹
uint8 int32
4 80
Printlnshows the numbers: 65, and 8377 (the rupee sign is Unicode character number 8377).%cis the Printf verb that shows the character for a number:Aand₹.- The types are
uint8andint32:byteandruneare just nicknames. len(city)is the length in bytes: 4.city[0]is the byte at index 0, which is 80, the number for capitalP. An index is a position, counted from zero.
Mixing types: Go never converts for you
Three cups of chai at Rs 12.50. cups is an int, price is a float64. Multiply them:
package main
import "fmt"
func main() {
cups := 3
price := 12.5
total := cups * price
fmt.Println(total)
}
./main.go:8:14: invalid operation: cups * price (mismatched types int and float64)
Most languages would quietly convert the int for you (an implicit conversion: one that happens without you writing it). Go refuses. Go has no implicit conversion, not even between two integer sizes:
package main
import "fmt"
func main() {
var small int32 = 10
var big int64 = 20
fmt.Println(small + big)
}
./main.go:8:17: invalid operation: small + big (mismatched types int32 and int64)
Annoying? Here's why Go is so strict.
Why Go is strict: a rocket and a hidden conversion
According to the University of Minnesota page "The Explosion of the Ariane 5":
- On 4 June 1996, the European rocket Ariane 5 launched for the first time.
- Inside its guidance software, a 64-bit floating-point number was converted into a 16-bit signed integer. The value was bigger than 32,767, the most a 16-bit signed integer can hold.
- The guidance failed, and about 40 seconds after lift-off the rocket was destroyed. The rocket and its cargo were valued at $500 million.
A conversion between types is a place where values can break. So Go makes you write every conversion yourself, where you and every reviewer can see it.
The fix: explicit conversion with T(value)
float64(cups) // int → float64
int(marks) // float64 → int (chops the decimals)
int64(small) // int32 → int64
- Explicit type conversion: write the type you want, and the value in round brackets. The type name is used like a function. Explicit means you wrote it on purpose.
int(...)of a float chops off everything after the decimal point (this is called truncation). It does not round.- It's still your job to make sure the value fits: a smaller type can wrap.
package main
import "fmt"
func main() {
cups := 3
price := 12.5
total := float64(cups) * price
fmt.Println(total)
marks := 9.99
fmt.Println(int(marks))
}
37.5
9
Rs 37.50. And 9.99 became 9, not 10: the decimals were chopped.
(Careful: int(9.99) written with the number directly, not a variable, is a compile error, because Go can see while compiling that the constant would lose its decimals: cannot convert 9.99 (untyped float constant) to type int. With a variable, the chop happens while running.)
Conversions can wrap too
package main
import "fmt"
func main() {
big := 200
fmt.Println(int8(big))
fmt.Println(uint8(big))
}
-56
200
200 doesn't fit in an int8 (max 127), so it wrapped: 200 - 256 = -56. The uint8 holds 200 fine. Go wrote the conversion in plain sight, but checking the range is still on you.
The string(65) trap, and strconv.Itoa
You have the number 65 and want the text "65". string(n) looks right:
package main
import "fmt"
func main() {
n := 65
fmt.Println(string(n))
}
A
string of a number gives the character with that number: 65 is capital A. Go's checker, go vet (a tool that looks for code that compiles but is probably a mistake), warns about exactly this line. Running go vet main.go on it prints:
./main.go:7:17: conversion from int to string yields a string of one rune, not a string of digits
To turn a number into its digits, use strconv.Itoa (from the strconv package; "I to A" = integer to ASCII, the opposite of Atoi from episode 3). ASCII is the old standard numbering for English letters, digits and symbols (A = 65); Unicode includes it.
package main
import (
"fmt"
"strconv"
)
func main() {
n := 65
fmt.Println(strconv.Itoa(n))
}
65
Sixty-five, as text.
Common mistakes (and the real error messages)
| You wrote | Real error | Fix |
|---|---|---|
var v int8 = 128 |
cannot use 128 (untyped int constant) as int8 value in variable declaration (overflows) |
use int16, or int |
var stock uint8 = -1 |
cannot use -1 (untyped int constant) as uint8 value in variable declaration (overflows) |
unsigned has no negatives; use int |
cups * price (int and float64) |
invalid operation: cups * price (mismatched types int and float64) |
float64(cups) * price |
small + big (int32 and int64) |
invalid operation: small + big (mismatched types int32 and int64) |
int64(small) + big |
var ok bool = 1 |
cannot use 1 (untyped int constant) as bool value in variable declaration |
var ok bool = true |
var cups int = 3.5 |
cannot use 3.5 (untyped float constant) as int value in variable declaration (truncated) |
use float64, or a whole number |
int(9.99) with a literal |
cannot convert 9.99 (untyped float constant) to type int |
convert a variable, or write 9 |
string(65) |
no error: prints A (go vet warns) |
strconv.Itoa(65) |
visitors++ past 127 (int8) |
no error: wraps to -128 | use a bigger type, like int |
stock-- below 0 (uint8) |
no error: wraps to 255 | check before subtracting |
money in a float64 |
no error: 0.1 + 0.2 is 0.30000000000000004 |
store paise in an int |
The silent ones are the dangerous ones: overflow while running gives no error, and float rounding gives no error.
Mini project: average marks
437 marks in total across 5 subjects. First the obvious way, then the right way.
package main
import "fmt"
func main() {
// Mini project: average marks
total := 437
subjects := 5
fmt.Println("wrong:", total/subjects)
avg := float64(total) / float64(subjects)
fmt.Printf("average: %.2f\n", avg)
}
wrong: 87
average: 87.40
- Dividing two
ints gives anint: 87. An int can't hold the .4, so it was dropped. int ÷ int = int. - Convert both to
float64, divide, and print with%.2f: the Printf verb for a float with exactly 2 digits after the decimal point.
Extension: percentage, chopped and rounded
package main
import (
"fmt"
"math"
)
func main() {
// Extension: percentage, chopped and rounded
total := 437
subjects := 5
maxPerSubject := 100
avg := float64(total) / float64(subjects)
percent := float64(total) / float64(subjects*maxPerSubject) * 100
fmt.Printf("average: %.2f\n", avg)
fmt.Printf("percentage: %.1f%%\n", percent)
fmt.Println("chopped:", int(avg), "rounded:", int(math.Round(avg)))
score := 87.5
fmt.Println("87.5 chopped:", int(score), "rounded:", int(math.Round(score)))
}
average: 87.40
percentage: 87.4%
chopped: 87 rounded: 87
87.5 chopped: 87 rounded: 88
%%in a Printf format prints a literal%sign.math.Roundrounds to the nearest whole number (halves go away from zero), but still returns afloat64, so we convert withint(...). Compare:int(87.5)chops to 87,math.Roundgives 88.
Practice
- Print the limits of
uint8,uint16,uint32anduint64using themathpackage. - Overflow an
int16on purpose and print the result. - Print
1.1 + 2.2and explain the result. Then try it with twofloat64variables. - Print the rune number of your initial (and of a Hindi letter, if you like).
Solutions and hints
- The minimum of every unsigned type is 0.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("uint8: ", 0, "to", math.MaxUint8)
fmt.Println("uint16:", 0, "to", math.MaxUint16)
fmt.Println("uint32:", 0, "to", math.MaxUint32)
fmt.Println("uint64:", 0, "to", uint64(math.MaxUint64))
}
uint8: 0 to 255
uint16: 0 to 65535
uint32: 0 to 4294967295
uint64: 0 to 18446744073709551615
Hint: fmt.Println(math.MaxUint64) without the uint64(...) fails, because the constant would land in an int: cannot use math.MaxUint64 (untyped int constant 18446744073709551615) as int value in argument to fmt.Println (overflows).
-
var n int16 = 32767thenn++thenfmt.Println(n)prints-32768: past the top of an int16, it wraps to the bottom. -
A surprise:
package main
import "fmt"
func main() {
fmt.Println(1.1 + 2.2)
a, b := 1.1, 2.2
fmt.Println(a + b)
}
3.3
3.3000000000000003
Written directly, 1.1 + 2.2 is worked out exactly by the compiler as a constant (episode 5 shows that constant maths is exact), so you see 3.3. With float64 variables, the sum happens while running, with the ruler's nearest marks, so you get 3.3000000000000003.
- Single quotes give a rune:
package main
import "fmt"
func main() {
initial := 'R'
fmt.Println(initial)
fmt.Printf("%c is %d, type %T\n", initial, initial, initial)
fmt.Println('अ')
}
82
R is 82, type int32
2309
Quick quiz
What does this print (inside main)?
var x uint8 = 250
x += 10
fmt.Println(x)
- A.
260 - B.
4 - C.
-6 - D. Compile error
Answer
B. 4. (x += 10 means x = x + 10.) A uint8 holds 0 to 255. 250 + 10 = 260, which wraps around past 255: 260 - 256 = 4. It is not a compile error, because the addition happens while the program runs.
Interview questions
- What is the difference between
int,int32andint64? How big isint? - What happens in Go when an integer overflows at run time? And when a constant is too big for its type?
- What is the difference between signed and unsigned integers? What does
uint8(0) - 1give while running? - Why is
0.1 + 0.2not exactly0.3? How should you store money? - What are
byteandrune? Why doeslen("Pune")count bytes? - Does Go do implicit type conversion? Why not? How do you convert an
intto afloat64? - What does
int(9.99)give for a variable? Does it round? - What does
string(65)return, and how do you get"65"instead? - What is
uintptrused for?
Glossary
| Term | Meaning | Example |
|---|---|---|
| Type | what kind of value a variable holds, and how big its box is | int8, string |
| Bit | one binary digit: a single on/off switch, 0 or 1 | 1 |
| Byte | 8 bits; can hold 256 different patterns | one uint8 |
| Binary | writing numbers with only 0 and 1 | 0111 1111 = 127 |
| Signed | can hold negative numbers | int8: -128 to 127 |
| Unsigned | no negatives; all patterns used for 0 and up | uint8: 0 to 255 |
| Sign bit | the leftmost bit of a signed integer: 0 = positive, 1 = negative | 1000 0000 = -128 |
| int | the default whole-number type; size depends on the platform | cups := 3 |
| int8 / int16 / int32 / int64 | signed integers of 8, 16, 32, 64 bits | var v int8 = 100 |
| uint family | unsigned integers: uint8, uint16, uint32, uint64, uint |
var stock uint8 |
| uintptr | unsigned integer big enough to hold a memory address | low-level code only |
| Memory address | the location of a value in memory, like a house number | |
| Platform-dependent int size | int and uint are 64 bits on 64-bit computers, 32 bits on 32-bit ones |
unsafe.Sizeof(n) = 8 here |
| Integer overflow | a result too big (or too small) for its type | int8 127 + 1 |
| Wraparound | after overflow the value jumps to the other end of the range, like an odometer | 127 + 1 → -128 |
| Silent wrap (panic-free) | run-time overflow in Go gives no error and no crash, just a wrong value | stock-- → 255 |
| Panic | Go stopping the program because something impossible happened while running (episode 6) | divide by zero |
| Compiling | checking the program and turning it into something the computer runs | go run compiles first |
| Compile error | an error found before the program starts; nothing runs | ... (overflows) |
| Untyped constant | a number written directly in code, before it has a fixed type (episode 5) | 128 |
| math package | standard package with maths functions and limits | math.MaxInt64 |
| math limits | constants for each type's smallest and biggest value | math.MinInt8, math.MaxInt64, math.MaxInt |
| unsafe.Sizeof | tells how many bytes a value takes in memory | unsafe.Sizeof(n) → 8 |
| ++ / -- | add one / subtract one from a variable (episode 6) | visitors++ |
| float32 | a decimal number in 32 bits, about 7 correct digits | var p float32 |
| float64 | a decimal number in 64 bits, about 15-16 correct digits; the default | price := 12.5 |
| Floating point | the way computers store numbers with a decimal point | 0.1 |
| Rounding error | a float is stored as the nearest possible value, not exactly | 0.30000000000000004 |
| Scientific notation | e+07 means "times 10 to the power 7" |
1.6777216e+07 |
| Paise | store money as whole paise in an int to avoid float errors |
Rs 12.50 → 1250 |
| complex64 / complex128 | complex numbers with float32 / float64 parts | complex(3, 4) |
| Real / imaginary part | the two parts of a complex number; i marks the imaginary one |
real(z), imag(z) |
| bool | a value that is only true or false |
ok := 5 > 3 |
| Comparison | an expression that asks a question and gives a bool | 5 > 3 |
| string | text, stored as a row of bytes | "Pune" |
| byte | another name for uint8; one byte, often a character |
'A' = 65 |
| rune | another name for int32; one Unicode character |
'₹' = 8377 |
| Alias | a second name for the same type | byte = uint8 |
| Unicode | the giant list giving every character in every script its own number | ₹ is 8377 |
| ASCII | the older numbering for English letters, digits and symbols, included in Unicode | A = 65 |
| Single vs double quotes | 'A' is one character (a rune); "A" is a string |
'₹', "Pune" |
| %c | Printf verb: print the character for a number | Printf("%c", 65) → A |
| %T / %.2f / %% | Printf verbs: type, float with 2 decimals, a literal % sign | Printf("%.2f", 87.4) |
| len | gives the length of a string in bytes | len("Pune") = 4 |
| Index | a position, counted from 0 | city[0] = 80 (P) |
| Mismatched types | the error when an expression mixes two different types | int and float64 |
| Implicit conversion | a type change done automatically, without you writing it; Go never does this | not in Go |
| No implicit conversion | Go's rule: different types never mix without a written conversion | int32 + int64 fails |
| Explicit type conversion | writing T(value) to change a value's type on purpose |
float64(cups) |
| Truncation | chopping off the decimal part instead of rounding | int(9.99) → 9 |
| math.Round | rounds a float to the nearest whole number | math.Round(87.5) = 88 |
| strconv.Itoa | turns an int into its digits as text ("integer to ASCII") | strconv.Itoa(65) → "65" |
| go vet | Go's checker for code that compiles but is probably a bug | warns on string(n) |
Summary
- A bit is a 0 or 1; a byte is 8 bits = 256 values. Every number type is a box of a fixed size.
int8,int16,int32,int64: bigger box, bigger range. Themathpackage has each limit.- Overflow wraps silently while running (Gangnam Style's int32 counter). A constant that doesn't fit is caught while compiling.
intis the default (64 bits here);uinttypes have no negatives and wrap to their top below zero;uintptrholds addresses.float64by default; floats are approximate, so never store money in them.complex128for complex numbers;boolis onlytrue/false, never 1.byte=uint8,rune=int32, astringis a row of bytes.- Go never converts types for you (remember Ariane 5). Write
T(value);int()chops; small types can wrap; usestrconv.Itoafor digits, notstring(n).
Next episode: constants and iota. A delivery app where order status 2 means something different on every screen. To fix it, you'll invent constants, and Go's little counting machine, iota.