Basic types

You have already met the four core types that carry most of the weight in any Go program:

  • int for whole numbers
  • float64 for numbers with a fractional part
  • string for text
  • bool for truth values

Everything else in Go (slices, structs, your own named types) is built on these atoms. This lesson walks through each one and calls out the decisions Go made that are different from other languages.

int and the integer family

int is Go's default integer type. Use it whenever you need a whole number and do not care about the exact bit width:

var count int = 42

The exact size of int depends on the platform (32 bits on a 32-bit system, 64 bits on a 64-bit system), but for everyday code that rarely matters. If you need explicit widths (for binary formats or tight memory), Go also offers int8, int16, int32, int64 and their unsigned versions uint8 through uint64. These are covered properly in the next lesson.

float64 and float32

float64 is Go's default floating-point type. It holds numbers with a fractional part, using the same IEEE 754 double-precision format used by JavaScript numbers and C double:

var price float64 = 19.95

float64 is accurate to about fifteen significant decimal digits. float32 also exists but is rare outside graphics code.

What does "fifteen significant digits" mean?

Significant digits are the total number of meaningful digits in a number, counting from the first non-zero digit. The number 19.95 has four significant digits. The number 0.00042 has two (4 and 2).

A float64 can hold about fifteen of them. That is plenty for coordinates, measurements, and most real-world values. You only run into trouble with numbers that need more than fifteen digits of detail, like 1.23456789012345678, where the last few digits quietly get rounded.

We talk more about the floating numbers in the next lesson, including the infamous 0.1 + 0.2 gotcha.

Just use int and float64

Unless a binary format, library signature, or performance constraint forces otherwise, reach for the defaults: plain int for whole numbers, float64 for decimals. The sized variants (int8, uint32, float32, and friends) are there when you need them, but starting with the defaults saves you from unnecessary conversions and keeps your code readable to other Go programmers.

string

A string holds text:

var name string = "coffee"

Two properties of strings often surprise people coming from other languages.

First, strings are immutable. Once created, you cannot change the bytes inside a string. Trying to replace a character in place is a compile error:

s := "hello"
s[0] = 'H'   // compile error: cannot assign to s[0]

You can build new strings from old ones by concatenating with + (which produces a new string and leaves the original untouched), but you cannot overwrite part of one in place. Indexing into strings is covered in detail in the Strings, runes, and bytes lesson later in this chapter.

Second, string literals use double quotes only. Single quotes are reserved for a different type called rune, which holds a single Unicode character. Strings, runes, and bytes get their own lesson later in this chapter.

bool

bool is the simplest type. Exactly two values:

var inStock bool = true

Go will not let you use an integer where a boolean is expected, unlike C. Assigning a number to a bool is a compile error:

var x int = 1
var active bool = x   // error: cannot use x (type int) as type bool

You have to compare it explicitly: var active bool = x != 0. This rules out a whole class of copy-paste bugs where a number is silently treated as a truth value.

No implicit conversions

Go does not silently convert between different types, not even between int and float64:

var a int = 3
var b float64 = 1.5
var c = a + b   // compile error, mismatched types

The next lesson walks through the conversion syntax. For now, remember: once a variable has a type, that is the type it keeps.

Quick reference

Type Holds Zero value Literal
int whole number, platform width 0 42
float64 decimal number 0 3.14
string immutable text "" "hello"
bool truth value false true, false

These four cover the majority of code you will write. Everything else in Go (slices, maps, structs, your own types) is built out of them.

Task

Declare one of each basic type using the var form with explicit types:

  • quantity, an int, set to 7
  • weight, a float64, set to 2.5
  • label, a string, set to "sugar"
  • available, a bool, set to false

Print all four on one line with fmt.Println.

Expected output
sugar 7 2.5 false