All Posts programming Learn Go | Tutorial 2: Storing a Thought (Variables)

Learn Go | Tutorial 2: Storing a Thought (Variables)

Β· 533 words Β· 3 minute read
go ▹

Right now, your app name and status text are frozen in the code. That works once, but as soon as you want to change the name or reuse the text, it becomes annoying. This “annoyance” is exactly the reason variables exist in any programming language, including Go.

Goal of this tutorial πŸ”—

By the end, your program will:

  • Store the app name and status message in variables.
  • Use fmt.Printf to print a formatted status line like:
    Go-Getter: Status = Online.

Step 1: Start from previous code πŸ”—

Open main.go in your go-getter folder. You should currently have something like:

package main

import "fmt"

func main() {
    fmt.Println("Go-Getter: System Online")
}

This works, but any time you want to:

  • Change the app name, or
  • Change the status message

you must edit the string literal directly. That is brittle and does not scale once the project grows.

Step 2: Introduce variables πŸ”—

Variables are named boxes that hold data in memory so you can reuse or modify it later.

There are two common ways to declare variables in Go:

  • Long form with var:
    var appName string = "Go-Getter"
    
  • Short form with := (inside functions only):
    appName := "Go-Getter"
    

Inside main, the short form is clean and idiomatic, so you will use it.

Update your main function like this:

package main

import "fmt"

func main() {
    appName := "Go-Getter"
    status := "Online"

    fmt.Println(appName, "Status:", status)
}

Run it:

go run main.go

You should see:

Go-Getter Status: Online

Now changing the app name or status is as simple as editing the variables, not the print call.

Step 3: Make the output more polished with Printf πŸ”—

fmt.Println prints each argument separated by a space and adds a newline at the end.

fmt.Printf, on the other hand, allows you to create a formatted string where placeholders like %s and %d are replaced by values.

For text, the %s verb is used for strings.

Change the print line to use Printf:

package main

import "fmt"

func main() {
    appName := "Go-Getter"
    status := "Online"

    fmt.Printf("%s: Status = %s\n", appName, status)
}

Explanation of the Printf call:

  • "%s: Status = %s\n" is the format string.
    • First %s β†’ app name.
    • Second %s β†’ status.
    • \n β†’ newline at the end.
  • appName, status are the values that fill those %s placeholders.

Run again:

go run main.go

Expected output:

Go-Getter: Status = Online

Step 4: Your mini-challenge πŸ”—

Do this without looking at the previous code:

  1. Add a new variable called version with a value like "v0.1".
  2. Change the print so it outputs:
    Go-Getter v0.1: Status = Online
  3. Use fmt.Printf and a third %s placeholder for the version.

You have now used variables to give your app an identity and a state instead of hardcoded constants. In the next tutorial, you will hit the “grocery list” problem and introduce slices and loops to manage multiple tasks at once.

I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .

go ▹