Go Lesson 5 – Variables | Dataplexa

Variables in Go

Variables are used to store data that a program can read, modify, and use during execution. In Go, variables are strongly typed, which means every variable has a fixed data type.

Go was designed to make variable declarations simple, readable, and safe. In this lesson, you will learn how to declare variables, assign values, and use them in real calculations.


What Is a Variable?

A variable is a named memory location that stores a value. For example:

  • A user’s age
  • An account balance
  • The number of active users
  • A product price

In Go, once a variable’s type is set, it cannot change.


Basic Variable Declaration

The most explicit way to declare a variable in Go uses the var keyword.

var age int
age = 25

Here:

  • age is the variable name
  • int is the data type
  • 25 is the assigned value

Declaring and Initializing Together

You can declare and assign a value in one line:

var salary int = 50000

This is commonly used when the initial value is known at creation time.


Type Inference in Go

Go can automatically infer the variable type from the assigned value.

var temperature = 36.6
var city = "San Francisco"

Go determines:

  • temperaturefloat64
  • citystring

Short Variable Declaration (:=)

Inside functions, Go provides a shorter and more popular syntax using :=.

count := 100
price := 29.99
name := "Dataplexa"

Important rules:

  • Can only be used inside functions
  • Go infers the type automatically
  • Cannot be used to re-declare an existing variable

Using Variables in a Program

Let’s see a real example using numeric values and calculations.

package main

import "fmt"

func main() {
    users := 120
    costPerUser := 15
    totalCost := users * costPerUser

    fmt.Println("Users:", users)
    fmt.Println("Cost per user:", costPerUser)
    fmt.Println("Total cost:", totalCost)
}

Output:

  • Users: 120
  • Cost per user: 15
  • Total cost: 1800

Multiple Variable Declaration

Go allows declaring multiple variables in a single statement.

var x, y, z int = 10, 20, 30

This is useful when variables are logically related.


Zero Values in Go

If a variable is declared but not initialized, Go assigns a default value called a zero value.

  • int → 0
  • float64 → 0.0
  • string → "" (empty string)
  • bool → false

Example:

var score int
fmt.Println(score)

Output:

  • 0

Why Go Is Strict About Variables

Go enforces strict variable usage to prevent bugs:

  • Unused variables cause compilation errors
  • Type mismatches are caught early
  • Cleaner, safer production code

This makes Go extremely reliable for large systems.


Real-World Use Case

In a billing system:

  • users → total subscribers
  • monthlyFee → subscription price
  • revenue → calculated using variables

All real Go applications rely heavily on variables like these.


Practice Exercise

Task

Create variables for:

  • Your name (string)
  • Your age (int)
  • Your monthly savings (float)

Print them using fmt.Println().


What You Will Learn Next

In the next lesson, you will learn about data types in Go, including integers, floats, booleans, and strings used in real applications.