What variables give you
A variable is a name for a stored value. The power of that idea is that the program refers to the name, not to the value itself. To change what the program does, you change the value in one place. You don't have to find and rewrite every line that uses it.
This lesson makes the point with a program that builds a sentence from two variables.
The example program
experience := 5
language := "Go"
fmt.Println("I have " + strconv.Itoa(experience) + " years of experience with " + language)
It prints:
I have 5 years of experience with Go
Several things are happening here, and each is worth understanding.
experience := 5declares anintvariable. The:=operator declares the variable and lets Go work out its type from the value, so no type is written.language := "Go"declares astring.- The
+operator joins strings, so each piece of the sentence has to be a string.experienceis a number, sostrconv.Itoa(experience)converts it to the text"5"first. Without that, Go would report a type mismatch and the program would not compile. (The nameItoastands for "integer to ASCII".) - A full program needs
import "strconv"for that call, as well asimport "fmt"for printing.
Change the value, change the story
The print statement never mentions 5 or "Go". It only mentions the names. So when you change the values, the same line tells a different story:
experience := 13
language := "Java"
fmt.Println("I have " + strconv.Itoa(experience) + " years of experience with " + language)
I have 13 years of experience with Java
Only two small edits were needed, and the print line stayed untouched. The output was rebuilt from the new values. The same idea scales up. A program that mentions a tax rate or a title in fifty places would be hard to update by hand, but if each of those places uses one variable, you edit one line.
One value, many uses
A variable can appear as often as you like. Suppose a program prints a greeting, a summary and a closing line, and all three use language. Changing the one declaration changes all three lines at once. Written as literal text, the same change would mean three edits, and missing one would leave the output inconsistent. This is the habit the lesson wants you to build: store a value once under a clear name, then refer to the name everywhere.
Declaring and changing
There are two different things you can do with a variable, and Go uses different symbols for them:
:=declares a new variable. It is used once, where the variable is created.=changes a variable that already exists.
In this exercise you are editing the declarations themselves, so the right-hand sides change and the left-hand sides stay as they are. If you wrote experience := 13 a second time in the same function, Go would report no new variables on left side of :=.
Matching types
A variable keeps the type it was given. experience started as an integer, so it must stay a whole number. If you replaced its value with "thirteen", Go would not compile the program, because a string can't be passed to strconv.Itoa, which expects an int. Likewise, language must keep holding text.
The exercise, step by step
- Find the two declarations. They are at the top, under the comment that says to create variables.
- Change the number. Replace
5with13and keepexperience :=as it is. - Change the text. Replace
"Python"with"Java", keeping the double quotes so it stays a string. - Leave the print line alone. It will now print the new sentence by itself.
// Create variables here:
experience := 13
language := "Java"
// Print story here:
fmt.Println("I have " + strconv.Itoa(experience) + " years of experience with " + language)
Key takeaways
- Code that uses variable names adapts when the stored values change.
:=declares a variable and infers its type; the type stays fixed afterwards.- To join a number to a string with
+, convert the number withstrconv.Itoa. - Edit the value to change the output, not the code that uses the variable.