Go Variables

Su Suriya Ravichandran Updated 14 Sep 2026
5 min read ·Lesson 5 of 12

A variable is a named storage location that holds a value your program can read, use in calculations, and update over time. Variables are how a Go program remembers and manipulates data as it runs.

Variable Types in Go

Go is a statically typed language: every variable has a specific, fixed type, determined either explicitly or by the compiler at the point of declaration. Once a variable's type is set, it cannot later hold a value of a different type.

Some of the most common built-in types:

  • int — whole numbers, e.g. 10, -5
  • float64 — decimal numbers, e.g. 3.14, -0.99
  • string — text, e.g. "Go Language"
  • booltrue or false

The full range of numeric and other basic types is covered in the Go Data Types lesson.

Declaring Variables

Go gives you two main ways to declare a variable.

1. The var Keyword

var variableName type = value
package main

import "fmt"

func main() {
    var language string = "Go"
    var version int = 1

    fmt.Println("Language:", language)
    fmt.Println("Version:", version)
}

var works both inside and outside functions, and its type can be written explicitly (as above) or inferred from the value — var version = 1 works just as well, since Go can tell 1 is an int.

2. Short Variable Declaration (:=)

variableName := value
package main

import "fmt"

func main() {
    framework := "Gin"
    users := 1000

    fmt.Println("Framework:", framework)
    fmt.Println("Active Users:", users)
}

:= declares a new variable and initializes it in one step, with the type always inferred from the value on the right. It is by far the most common way to declare variables inside functions in idiomatic Go — but it comes with two restrictions worth remembering:

  • It can only be used inside functions, never at package level.
  • It always requires an initial value; you cannot write x := with nothing on the right.

Declaring With and Without Initial Values

You can declare and assign in one line:

package main

import "fmt"

func main() {
    var appName string = "Task Manager"
    var users = 250    // type inferred as int
    sessions := 75     // short declaration

    fmt.Println(appName)
    fmt.Println(users)
    fmt.Println(sessions)
}

If you declare a variable with var and don't assign a value, Go automatically initializes it to that type's zero value — Go never leaves a variable in an undefined or garbage state, unlike some lower-level languages.

TypeZero value
string"" (empty string)
int0
boolfalse
package main

import "fmt"

func main() {
    var title string
    var count int
    var isActive bool

    fmt.Println("Title:", title)     // ""
    fmt.Println("Count:", count)     // 0
    fmt.Println("Active:", isActive) // false
}

You can assign a value later, once it's known:

package main

import "fmt"

func main() {
    var city string
    city = "Chennai"
    fmt.Println("City:", city)
}

var vs :=

Featurevar:=
ScopeWorks inside and outside functionsOnly inside functions
Type specificationOptional (can be explicit or inferred)Always inferred
Declaration without assignmentAllowedNot allowed
Typical usePackage-level variables, or when you want a zero-value defaultLocal variables with a known initial value

Package-Level (Global) Variables

var is required for variables declared outside any function:

package main

import "fmt"

var appVersion int = 2
var appName = "Inventory System"

func main() {
    var users int
    users = 50

    fmt.Println("App:", appName)
    fmt.Println("Version:", appVersion)
    fmt.Println("Users:", users)
}

:= cannot be used here — it is only valid inside a function body.

Declaring Multiple Variables at Once

Same Type, One Line

package main

import "fmt"

func main() {
    var x, y, z int = 10, 20, 30

    fmt.Println("x:", x)
    fmt.Println("y:", y)
    fmt.Println("z:", z)
}

Mixed Types, Inferred

When the type is omitted, Go infers each variable's type independently from its own value:

package main

import "fmt"

func main() {
    var id, name = 101, "Alice"
    score, passed := 88.5, true

    fmt.Println("ID:", id)
    fmt.Println("Name:", name)
    fmt.Println("Score:", score)
    fmt.Println("Passed:", passed)
}

Grouped Declarations

For readability, var supports a block form, which is especially useful when declaring several related variables together:

package main

import "fmt"

func main() {
    var (
        age     int
        salary  float64 = 45000.50
        country string  = "India"
        active  bool    = true
    )

    fmt.Println("Age:", age)
    fmt.Println("Salary:", salary)
    fmt.Println("Country:", country)
    fmt.Println("Active:", active)
}

This groups related configuration or state together, which pays off as the number of variables grows.

Go Variable Naming Rules

Go enforces a small set of hard rules, plus some strong naming conventions.

Hard rules (violating these is a compile error):

  • A name must start with a letter (az, AZ) or an underscore (_) — never a digit.
  • After the first character, only letters, digits, and underscores are allowed — no @, #, -, or spaces.
  • Names are case-sensitive: total, Total, and TOTAL are three different identifiers.
  • Reserved keywords (var, func, package, if, for, and 22 others) cannot be used as variable names.
package main

import "fmt"

func main() {
    // Valid variable names
    var userName string = "Arun"
    var _count int = 5
    var totalPrice float64 = 199.99

    // Invalid examples (uncomment to see compiler errors)
    // var 1stPlace int = 1     // starts with a digit
    // var user-name string    // contains a hyphen
    // var full name string    // contains a space

    fmt.Println(userName, _count, totalPrice)
}

Convention (not enforced by the compiler, but expected in idiomatic Go code):

  • camelCasestudentName, totalMarks — the standard style for local variables and unexported package-level names.
  • PascalCaseStudentName — reserved in Go for exported identifiers: a name starting with a capital letter is automatically visible outside its package, while a lowercase name stays private to the package. This capitalization rule is unique to Go and is covered further once you reach functions and packages.
  • snake_casestudent_name — rarely used in Go; it's more common in languages like Python or Ruby, and most Go style guides recommend avoiding it in favor of camelCase.

Common mistake: using PascalCase for a variable that should stay internal to a package. Because capitalization controls visibility in Go, an accidental capital letter can unintentionally expose an internal variable as part of your package's public API.

0 Comments

Reviewed before they appear

No comments yet.

Go Lang
Ask about this post
AI Ask about this post

Ask questions about Go Variables and get answers drawn from it.

Signed-in readers only.