Go variables are the first thing you need after setting up Go, because every program stores some data. In this post, you will learn how to create variables and constants. You will also meet the data types you use every day. Each idea comes with a small example you can run.
If you are new to the language, start with my guide on what Go is and where it is used. Also, these topics come up in almost every interview, so my Go interview questions and answers page is good practice later.
What Are Go Variables?
A variable is a named box that holds a value. Think of a labelled jar in your kitchen. The label is the name, and the sugar inside is the value. You can open the jar later and use what is inside.
Go is a statically typed language. This means every box has a fixed type, such as number or text. So a box made for numbers can never hold text. This rule catches many bugs before your program even runs.
How to Declare Go Variables
To declare a variable means to create it and give it a name. Go gives you two main ways to do this.
Using the var Keyword
var name string = "Smita"
var age int = 30
fmt.Println(name, age) // Smita 30
First comes the word var, then the name, then the type, and finally the value. This form works everywhere, both inside and outside functions.
Using the Short Form :=
city := "Pune"
price := 99.50
isActive := true
fmt.Println(city, price, isActive) // Pune 99.5 true
Here you skip both var and the type. Instead, Go looks at the value and picks the type for you. We call this type inference. Most Go developers use this short form inside functions, because it is quick to write.
Declaring Many Go Variables at Once
var (
firstName = "Asha"
score = 85
)
x, y := 10, 20
The var ( ) block groups related values together, so your code stays tidy. Also, the second line shows how to create two Go variables in one step.
Zero Values: What Go Variables Hold by Default
What happens if you create a variable but give it no value? In many languages, you get garbage or an error. However, Go fills it with a safe starting value called the zero value.
var count int
var total float64
var title string
var done bool
fmt.Println(count, total, title, done) // 0 0 false
| Type | Zero value |
|---|---|
| int, float64 | 0 |
| string | “” (empty text) |
| bool | false |
| pointer, slice, map | nil (nothing yet) |
Notice the two spaces in the output. That gap is the empty string sitting between 0 and false.
Basic Data Types in Go
A data type tells Go what kind of value a box can hold. Here are the types you will use most of the time.
| Type | Holds | Example |
|---|---|---|
| int | Whole numbers | 30, -7 |
| float64 | Decimal numbers | 99.50 |
| string | Text | “Pune” |
| bool | True or false | true |
| byte | One byte of data (same as uint8) | ‘A’ |
| rune | One Unicode character (same as int32) | ‘₹’ |

Every variable has a name, a type and a value. Each type also has its own zero value.
Want to check a type yourself? Use %T with fmt.Printf, and Go prints the type name.
fmt.Printf("%T %T %T\n", city, price, isActive)
// string float64 bool
Type Conversion
Go never mixes types on its own. So if you add an int to a float64, you must convert one of them first.
apples := 5
weight := 2.75
sum := float64(apples) + weight
fmt.Println(sum) // 7.75
fmt.Println(int(weight)) // 2
text := "Count: " + strconv.Itoa(apples)
fmt.Println(text) // Count: 5
Look at int(weight). It cuts off the decimal part, so 2.75 becomes 2. Also, to turn a number into text, use strconv.Itoa from the strconv package.
Constants in Go
A constant is a value that never changes, like the number of days in a week. You create it with const instead of var.
const Pi = 3.14
const (
Small = iota // 0
Medium // 1
Large // 2
)
The word iota is a counter that starts at 0. Then it goes up by one on each new line inside the block. This is handy for sizes, levels or status codes.
If you try Pi = 3.15 later, the compiler stops with an error. That is the whole point, because a constant protects values that must stay fixed.
Common Mistakes with Go Variables
| Error you see | Why it happens | How to fix it |
|---|---|---|
| declared and not used: unused | You created a local variable but never used it. | Use the variable or delete it. |
| syntax error: non-declaration statement outside function body | You used := outside a function. | Use var at package level. |
| invalid operation: mismatched types int and float64 | You mixed two number types. | Convert one side, for example float64(x). |
The first error surprises most beginners. Still, it is a good habit, because unused Go variables usually point to a bug. One more tip: names starting with a capital letter, like Total, are visible to other packages. Lowercase names stay private.
Official Resources
These pages come straight from the Go team, so they are always correct.
Quick Recap
- Go variables are named boxes with a fixed type and a value.
- Use var anywhere, and the short form := inside functions.
- A variable with no value gets a zero value: 0, “”, false or nil.
- Go never mixes types, so convert with float64(x), int(y) or strconv.Itoa.
- Constants use const and cannot change, and iota counts up from 0.
What’s Next?
Now you can store data in Go. Next, we will use that data with operators, so you can add, compare and combine values.
Coming soon: Go Operators with Simple Examples
Later in this series, you will save these values in a real database. If you are curious, my database interview questions post explains tables, keys and joins.


