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.
Ten steps. Let's go.
What you'll learn in this Go variables tutorial
This interactive Go tutorial has 10 hands-on exercises. Estimated time: 15 minutes.
- What is a variable — Imagine a box with a label on it.
- Create your own variable with := — `:=` is how you create a variable and give it a value at the same time.
- Declare first, assign later — Sometimes you want to create the box first and fill it later.
- The var keyword with a value — `var` can also declare and assign in one line — just like `:=` but more explicit.
- Update a variable — Variables can change. That is literally what the word means.
- Two variables in one line — Go lets you declare and assign multiple variables at once.
- Declare a group with var () — When you have several variables, you can declare them as a group.
- Zero values — What happens if you create a variable but never put anything in it?
- Fix the broken names — Variable names have rules:
- Build a profile from scratch — Everything you just learned — now use it all.
Go Variables concepts covered
- Storing information