Basic

Primitive Types

Understand Go's built-in types: integers, floats, strings, and booleans.

What you will see

float->int: 3

About 2 min of reading

Try It

Go has a fixed family of numeric types and converts between them only when you ask. You will build values of different sizes, see Go inferring float64 and bool, and watch explicit conversions truncate a float back to an int.

1

I declare several integer types and let Go print them: the default int, an int8, a uint16, and an int64. Each name carries its chosen size, and I read the four values on one line.

package main import "fmt" func main() { var a int = 42 var b int8 = 7 var c uint16 = 100 var d int64 = 1000000 fmt.Println(a, b, c, d) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
42 7 100 1000000
Choose the smallest type that fits your data: int8 uses 1 byte while int64 uses 8, which matters a lot in large collections.
2

I work with decimals and text. float64 is Go's default for decimals, and strings are fixed sequences of text. I print each value to see the type each one inferred.

package main import "fmt" func main() { price := 9.99 pi := 3.14159 name := "Ada Lovelace" fmt.Println(price, pi, name) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
9.99 3.14159 Ada Lovelace
float64 is the default decimal type; use float32 only when you explicitly want a smaller, less precise float.
3

I set two booleans. Go's bool holds exactly true or false, and an if condition must be a real bool, there is no truthy-number trick like in some other languages.

package main import "fmt" func main() { isActive := true hasAccess := false fmt.Println(isActive, hasAccess) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
true false
An if in Go will not accept a bare number or string as a condition; it must be a genuine bool.
4

I convert an int to a float. Go does not convert between numeric types on its own, so I must wrap the value in the target type: float64(i).

package main import "fmt" func main() { i := 42 f := float64(i) fmt.Println("int->float:", f) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
int->float: 42
Converting int to float64 keeps the exact value here, but the reverse conversion loses precision.
5

I convert back, from float to int. This truncates toward zero, so 3.99 becomes 3 and never rounds to 4. I print the result to confirm the loss.

package main import "fmt" func main() { f := 3.99 i := int(f) fmt.Println("float->int:", i) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
float->int: 3
int(3.99) is 3, not 4, conversion truncates toward zero, so always decide what to do with the discarded part before converting.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A literal is stored in a typed slot; converting changes the representation but keeps the value.
1 Literal stored in a typed variable
↓
2 Conversion requested with float64(i)
↓
3 The value is re-encoded into the new type
↓
4 The new value can be used in calculations
Why this works
Type sizes

Go lets you control memory with different integer sizes:

TypeBytesRange
int81-128 to 127
int162-32,768 to 32,767
int324±2 billion
int648huge

Choose the smallest type that holds your values to save memory in large collections.

Type conversion

Go does not convert automatically between numeric types. You must convert explicitly:

i := 42
f := float64(i)   // int to float
j := int(f)       // float back to int (truncates)

int(f) truncates toward zero, int(3.99) is 3, not 4. Always convert deliberately.

Examples

Integers

Whole numbers

var a int = 42
var b int8 = 7
var c uint16 = 100
var d int64 = 1000000

int is the default and matches the platform (64-bit on most machines). int8, int16, int32, int64 choose a size. uint variants are unsigned.

Why it works: smaller types use less memory but have a limited range, pick the smallest that fits your data.

Floats and strings

Decimals and text

price := 9.99
pi := 3.14159
name := "Ada Lovelace"

float64 is the default for decimals. Strings are immutable sequences of UTF-8 bytes, once created, they cannot change.

Watch out: Go is strictly typed. You cannot add an int and a float64 directly, convert first.

Booleans

True and false

isActive := true
hasAccess := false

bool holds exactly true or false. Unlike some languages, Go has no truthy/falsy, an if condition must be a real boolean, not a number or string.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Variables and Constants
Previous Lesson
Variables and Constants
Next Lesson
Conditionals and Branching
Conditionals and Branching →