Learn Go | Tutorial 2: Storing a Thought (Variables)
- Part 1: Learn Go | Tutorial 1 (The Handshake): Your First Go Program
- Part 2: Learn Go | Tutorial 2: Storing a Thought (Variables)
- Part 3: Learn Go | Tutorial 3: The Grocery List Problem
- Part 4: Learn Go | Tutorial 4: Listening to the User
- Part 5: Learn Go | Tutorial 5: Making Choices (The Command Loop)
- Part 6: Learn Go | Tutorial 6: The Memory Loss Issue (File Persistence)
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.Printfto 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.
- First
appName, statusare the values that fill those%splaceholders.
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:
- Add a new variable called
versionwith a value like"v0.1". - Change the print so it outputs:
Go-Getter v0.1: Status = Online - Use
fmt.Printfand a third%splaceholder 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 .
- Part 1: Learn Go | Tutorial 1 (The Handshake): Your First Go Program
- Part 2: Learn Go | Tutorial 2: Storing a Thought (Variables)
- Part 3: Learn Go | Tutorial 3: The Grocery List Problem
- Part 4: Learn Go | Tutorial 4: Listening to the User
- Part 5: Learn Go | Tutorial 5: Making Choices (The Command Loop)
- Part 6: Learn Go | Tutorial 6: The Memory Loss Issue (File Persistence)