Go Tutorial: Variables
Store information in your programs so you can use it, change it, and reuse it.
Storing information
Until now your programs have had fixed text baked directly into the code.
In this chapter you will learn how to store information so your program can work with it, change it, and reuse it in different places.
That is what a variable is for.
Seven steps. Let's go.
What you'll learn in this Go variables tutorial
This interactive Go tutorial has 7 hands-on exercises. Estimated time: 15 minutes.
- What is a variable — A variable is a labeled slot that holds a value — like a shelf slot labeled with a book title. Run this to see one in ac…
- Create your own with := — `:=` creates a variable and gives it a value in one shot. Change the title to your favorite book and run it.
- Declare first, assign later — `var title string` creates an empty slot. `title = "The Hobbit"` puts a book in it. Note: filling later uses `=`, not `:…
- Update a variable — the aha 🤯 — Variables can change. `:=` creates, `=` updates. The count starts at 60. A shipment arrives — update it to 95 and print …
- Two variables in one line — Go lets you declare multiple variables at once: `title, author := "The Hobbit", "Tolkien"`. Change the book and author t…
- Zero values — What happens if you create a shelf slot but never put a book in? Go gives it a default called a zero value. Run this to …
- Build a library profile — Use everything you learned: create `title` and `copies` with `:=`, declare `section` with `var` and assign it separately…
Go Variables concepts covered
- Storing information