---
title: "Variables, types and constants in Go"
slug: "variables-and-types"
description: "Declare variables with var and :=, see zero values and types printed with %T, convert between numbers and use const. Fix a mismatched-types error."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-go/variables-and-types"
---

# Variables, types and constants in Go

How does Go know the type of a variable?

## Predict

```go
package main

import "fmt"

func main() {
	x := 7 / 2
	y := 7 / 2.0
	fmt.Println(x, y)
}
```

What does this program print?

- 3.5 3.5
- 3 3.5
- It does not compile

Answer: 3 3.5 — In the first line both numbers are integers, so the result is an integer; in the second, 2.0 makes it a float.

Output:

```
3 3.5
```

The kind of number you write decides the type: 7 / 2 is integer division, 7 / 2.0 is a float.

## var and :=

Declare a variable with `var name type`, or let Go infer the type: `var name = "tea"`. Inside a function the short form `x := 7` does the same and is what most Go code uses. A variable you declare without a value gets its **zero value**: `0` for numbers, `""` for strings, `false` for booleans.

## Types and zero values

```go
package main

import "fmt"

func main() {
	var count int
	price := 9.5
	var name = "tea"
	var ready bool
	fmt.Printf("%T %v\n", count, count)
	fmt.Printf("%T %v\n", price, price)
	fmt.Printf("%T %q\n", name, name)
	fmt.Printf("%T %v\n", ready, ready)
}
```

Output:

```
int 0
float64 9.5
string "tea"
bool false
```

`%T` prints the type and `%v` the value; `%q` prints a string in quotes. `count` and `ready` were never set, yet hold zero values.

Playground: https://go.dev/play/p/1AB_d0WOyy4

## Conversions and constants

Go never converts between number types for you. To mix an `int` and a `float64`, convert one: `float64(n)`. Strings and numbers convert with the `strconv` package: `strconv.Itoa` turns an int into text and `strconv.Atoi` parses text into an int and an error. A `const` declares a value that never changes.

## Convert, use a const and parse text

```go
package main

import (
	"fmt"
	"strconv"
)

const taxRate = 0.2

func main() {
	items := 3
	net := float64(items) * 4.5
	fmt.Println(net, net*(1+taxRate))
	label := "Items: " + strconv.Itoa(items)
	fmt.Println(label)
	n, err := strconv.Atoi("42")
	fmt.Println(n+1, err)
}
```

Output:

```
13.5 16.2
Items: 3
43 <nil>
```

`items` is an int, so `float64(items)` is needed before multiplying by a decimal. `Atoi` returns the number and an error, which is `<nil>` when parsing worked.

Playground: https://go.dev/play/p/3d3Aq0_Q0h0

## Fix it

```go
package main

import "fmt"

func main() {
	var total int = 10
	var ratio float64 = 0.5
	fmt.Println(total * ratio)
}
```

The compiler rejects this program. What is the bug?

- fmt.Println cannot print a number
- total is an int and ratio is a float64, and Go will not multiply them
- The variables must be declared with := instead of var

Answer: total is an int and ratio is a float64, and Go will not multiply them — The compiler reports mismatched types int and float64. Convert one side first.

Fixed:

```go
package main

import "fmt"

func main() {
	var total int = 10
	var ratio float64 = 0.5
	fmt.Println(float64(total) * ratio)
}
```

Output:

```
5
```

## Say it back

In your own words: what is a zero value, and why does Go have no uninitialised variables?

Our sentence: A zero value is what Go gives every variable you declare without a value, such as 0 or an empty string, so a variable always holds something well defined.

## Mission (8 min)

Write a program that declares an int, a float64, a string and a bool without values, prints them with %v, then converts the int to float64 and multiplies it by the float.

```sh
go run .
```

## Common questions

### How does Go know the type of a variable?

When you write var name = "tea" or name := "tea", Go takes the type from the value on the right, here string. This is called type inference. You can also name the type yourself, as in var count int. Either way, the variable keeps that single type for its whole life, and the compiler rejects code that mixes types.

### What is the difference between var and := in Go?

Both declare a variable. The var form works anywhere, including at package level, and lets you give a type with or without a value. The short form := needs a value and works only inside functions, which makes it the common choice for local variables. Use var when you want a zero value or an explicit type.

### How do I convert an int to a float64 in Go?

Write the type name like a function: float64(n) turns the int n into a float64. Go does not convert numbers for you, so you must do it before mixing an int and a float64 in one expression. To go the other way use int(x), which drops the fraction. To turn a number into text, use the strconv package.

## Sources

- https://go.dev/ref/spec#Variable_declarations (read 2026-12-03)
- https://go.dev/ref/spec#Conversions (read 2026-12-03)
- https://go.dev/ref/spec#Constant_declarations (read 2026-12-03)

Measured with go1.27.4 darwin/arm64 on 2026-12-03.
