Variables, types and constants in Go
Learn Go · Beginner · 10 min · After Install Go and run your first program
How does Go know the type of a variable?
1Guess
package main import "fmt" func main() { x := 7 / 2 y := 7 / 2.0 fmt.Println(x, y) }2Read
var and :=
Declare a variable with
var name type, or let Go infer the type:var name = "tea". Inside a function the short formx := 7does the same and is what most Go code uses. A variable you declare without a value gets its zero value:0for numbers,""for strings,falsefor booleans.3Code
Types and zero values
1 / 2package 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) }Read the program. What will it print?
4Read
Conversions and constants
Go never converts between number types for you. To mix an
intand afloat64, convert one:float64(n). Strings and numbers convert with thestrconvpackage:strconv.Itoaturns an int into text andstrconv.Atoiparses text into an int and an error. Aconstdeclares a value that never changes.5Code
Convert, use a const and parse text
1 / 2package 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) }Read the program. What will it print?
6Fix
package main import "fmt" func main() { var total int = 10 var ratio float64 = 0.5 fmt.Println(total * ratio) }7Say it back
8Mission · 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.
go run .
Free AI coding tools to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Cline Free Models
Try select models free in Cline via rotating promotions with a usage quota
No cardVerified
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
Published Dec 3, 2026 · Updated Dec 3, 2026