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 8 hands-on exercises. Estimated time: 20 minutes.
- What is a data type — Every variable has a type that determines what kind of data it holds — like different library shelves for different kind…
- string — text in quotes — A `string` holds text — book titles, author names, ISBNs. Always wrapped in double quotes. Create a variable called `boo…
- int and float64 — numbers — `int` holds whole numbers (no decimal). `float64` holds numbers with decimals. Create `totalCopies` as int (42) and `boo…
- bool — true or false — A `bool` holds exactly one of two values: `true` or `false`. No quotes. Create `isCheckedOut := true` and `isReferenceOn…
- Type inference — Go figures it out — You have been using `:=` without writing the type name. Go reads the value and picks the type automatically. Run this an…
- "42" is not 42 — quotes matter — `42` is a number you can do math with. `"42"` is a string — the characters 4 and 2. The code below tracks checked-out bo…
- Type conversion — change types explicitly — Go will not automatically mix types. Convert with `float64(myInt)` or `int(myFloat)`. Fix the code by converting `booksO…
- Use six types in one program — Create six variables using six different types: `sectionName` (string), `totalShelves` (int), `members` (int64), `lateFe…
Go Data Types concepts covered
- Every value has a type