Go Tutorial: Data Types
Every value in Go has a type. Learn all of them — strings, integers, floats, bools, bytes, and runes.
Every value has a type
In the last chapter you created variables. But every variable holds a specific kind of data.
A name is not the same kind of thing as an age. An age is not the same as a price. Go needs to know exactly what kind of data you are working with — because what you can do with it depends entirely on what type it is.
This chapter covers every basic type in Go. Fourteen steps.
What you'll learn in this Go data types tutorial
This interactive Go tutorial has 14 hands-on exercises. Estimated time: 20 minutes.
- What is a data type — Think about a moving truck.
- string — text — A `string` holds text. Any text. A single letter, a word, a sentence, an email address.
- int — whole numbers — `int` holds whole numbers. No decimal point. Ages, counts, scores, years.
- int8, int16, int32, int64 — sized integers — Sometimes you need to control exactly how much memory a number uses. That is what sized integers are for.
- uint — unsigned integers — Regular `int` can hold negative numbers. But sometimes your value can never be negative — a file size, a pixel count, an…
- float32 and float64 — decimal numbers — When a whole number is not precise enough, you need a float.
- bool — true or false — A `bool` holds exactly one of two values: `true` or `false`.
- byte — a single character as a number — `byte` is an alias for `uint8`. It holds a number from 0 to 255.
- rune — a Unicode character — `rune` is an alias for `int32`. It holds a single Unicode character.
- Type inference — Go figures it out — You have been using `:=` without ever writing the type name. That is because Go looks at the value and figures out the t…
- "42" is not 42 — This is a trap that catches a lot of beginners.
- Type conversion — changing from one type to another — Go will not automatically mix types. If you want to use an `int` where a `float64` is needed, you have to convert it you…
- Types do not change — Once a variable has a type, it keeps that type forever.
- Use six types in one program — No starter code this time.
Go Data Types concepts covered
- Every value has a type