Start learning free

LESSON 9 OF 338 · VARIABLES

The Power of Variables in Go

Lesson Read the full explanation

Variables let us change a program's behavior by changing stored values instead of rewriting the whole program.

For example, this program builds a sentence from two variables:

experience := 5
language := "Go"

fmt.Println("I have " + strconv.Itoa(experience) + " years of experience with " + language)

If experience or language changes, the printed story changes automatically.


Assignment
Change the variables so the story says the person has 13 years of experience with Java. Do not change the print statement.


Hint
Use experience := 13 and language := "Java".


STEP 1 OF 1
    Ctrl + Enter to check

    GO · VARIABLES

    The Power Of Variables in Go, explained

    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 := 5 declares an int variable. The := operator declares the variable and lets Go work out its type from the value, so no type is written.
    • language := "Go" declares a string.
    • The + operator joins strings, so each piece of the sentence has to be a string. experience is a number, so strconv.Itoa(experience) converts it to the text "5" first. Without that, Go would report a type mismatch and the program would not compile. (The name Itoa stands for "integer to ASCII".)
    • A full program needs import "strconv" for that call, as well as import "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

    1. Find the two declarations. They are at the top, under the comment that says to create variables.
    2. Change the number. Replace 5 with 13 and keep experience := as it is.
    3. Change the text. Replace "Python" with "Java", keeping the double quotes so it stays a string.
    4. 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 with strconv.Itoa.
    • Edit the value to change the output, not the code that uses the variable.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free