Now that our project is set up, it’s time to make it useful: let’s log our first expenses. An expense tracker without input is just a blank page — we want a tool that listens to us, records our daily spending, and gives us control.
Go makes this simple. Even as beginners, we can build something that feels practical and “real” with just a few lines of code.
Step 1: Reading Input from the Console
In Go, we can use the bufio package to read what the user types. Here’s the simplest example:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a description: ")
text, _ := reader.ReadString('\n')
fmt.Println("You wrote:", text)
}
What’s happening here?
bufio.NewReader(os.Stdin)creates a reader connected to the keyboard.ReadString('\n')waits until you press Enter and then gives you the text.fmt.Printandfmt.Printlndisplay messages in the terminal.
Run the program, type something, and see it echo back to you. That’s your first conversation with the program.
Step 2: Asking for an Expense
Let’s extend the program so the user can enter both a description and an amount:
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter expense description: ")
description, _ := reader.ReadString('\n')
description = strings.TrimSpace(description)
fmt.Print("Enter amount (in euros): ")
amountStr, _ := reader.ReadString('\n')
amountStr = strings.TrimSpace(amountStr)
amount, err := strconv.ParseFloat(amountStr, 64)
if err != nil {
fmt.Println("Error: please enter a valid number.")
return
}
fmt.Println("Expense recorded:", description, "-", amount, "€")
}
Here’s what’s new:
strings.TrimSpace()removes spaces and newlines.strconv.ParseFloat()converts text into a number (e.g.,10.50).- If the user types something wrong, the program shows an error instead of crashing.
Step 3: Motivation for Beginners
Why is this exciting?
- Because you’ve just built a real tool — type an expense, and it responds.
- With a few more steps, this tool will save your data and help you track spending.
- Unlike bloated apps, this is yours: lightweight, private, and under your control.
Every line of Go you write here is an investment: you’re not just learning syntax, you’re building something that grows with you.
At this point, you can run the program, enter a few test expenses, and see them printed back. In the next chapter, we’ll make the tracker even more powerful by saving your expenses into a local JSON file, so they don’t disappear when you close the program.
