By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A variable is a name for a box that holds one value. You write the name in your code, and Go reads or changes what is inside. Most of the programs you write from now on are boxes being filled, read and replaced.
Two things describe a box you create. The name is the label you choose, like total or item. The type decides what kind of value fits: an int box holds whole numbers, a string box holds text. Once the box exists its type does not change, which lets the compiler warn you early when the wrong kind of value heads for it.
Go gives you three forms. The table shows what each one looks like and when a beginner reaches for it.
| Form | Looks like | When to use it |
|---|---|---|
var | var total int | When you want the type written out, or need a box before you know its value |
var with a value | var total int = 1250 | When you want the type written out and a starting value |
:= | total := 1250 | Inside a function, when Go can work out the type from the value |
const | const centsPerDollar = 100 | For a value that must never change |
The short form := is the one you will type most. It declares a name and assigns a value in one line, and Go works out the type from the value. It only works inside a function. The long var form is useful when you want the type written out, or when you want to declare a box before you know what goes in it.
package main
import "fmt"
func main() {
var total int
total = 1250
fmt.Println("total:", total)
item := "coffee"
days := 3
fmt.Println(item, "tracked for", days, "days")
}| Type | Holds | Example |
|---|---|---|
int | Whole numbers, positive or negative | 1250 |
float64 | Numbers with a decimal point | 12.5 |
string | Text | "coffee" |
bool | true or false | true |
String values wear double quotes. Numbers and booleans do not. A bool has only two possible values, true and false, and step 9 shows why two values are enough to drive every decision a program makes.
When you declare a variable and give it no value, Go does not leave it undefined or random. It fills the box with that type's zero value. This is one of the small rules that makes Go programs predictable: a variable always holds a valid value of its type, from the moment it exists.
package main
import "fmt"
func main() {
var count int
var price float64
var label string
var paid bool
fmt.Println("count:", count)
fmt.Println("price:", price)
fmt.Println("label:", label)
fmt.Println("paid:", paid)
}The value is real, not missing. count is 0, price is 0, paid is false, and label is the empty string: a string with no characters. The zero values are 0 for int, 0 for float64, "" for string and false for bool.
A constant is a value fixed when the program is built. Use const for facts that must never change while the program runs: how many cents are in a dollar, a fixed greeting, a limit nobody should edit by accident.
package main
import "fmt"
func main() {
const centsPerDollar = 100
dollars := 12
fmt.Println(dollars, "dollars is", dollars*centsPerDollar, "cents")
dollars = 15
fmt.Println(dollars, "dollars is", dollars*centsPerDollar, "cents")
}Notice that the multiplication is written twice but the constant is written once. Change the constant and both lines follow. That is the point of giving an important fact a name.
Inside main, create boxes for one expense: a string name set to "coffee", an int price in cents set to 450, a float64 rate set to 0.0837, and a bool named paid set to true. Print each one on its own line with a short label, the way the zero-value example does, and run it. Then rewrite the declarations with := and run it again: the output should not change. Finally, declare one more variable with var and no value, print it, and check you can name its zero value.
Key takeaways
A variable is a named box, and its type fixes what kind of value it can hold.
var states the type, := infers it inside a function, and const fixes a value for good.
A declared variable that you do not fill holds its type's zero value: 0, 0.0, "", or false.
Go refuses to build code that creates a local variable and never reads it.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.