Variables

Contents
Declare with var
Short := declaration
Zero values
Naming rules

Declare with var

The var keyword declares a variable with an explicit type and an optional initial value.

For the full reference see the article declare variables and use the pattern below as the default form.

package main import "fmt" func main() { var name string = "Gopher" var age int = 5 var active bool = true fmt.Println(name, age, active) }

go run .

Gopher 5 true

Short := declaration

Inside functions, the short form with := declares the variable and infers its type from the value on the right side.

It keeps code compact, but it cannot be used outside functions or without a new value.

package main import "fmt" func main() { name := "Gopher" age := 5 ratio := 0.5 fmt.Println(name, age, ratio) }

go run .

Gopher 5 0.5

Zero values

Every declared variable already holds a zero value before the first assignment: 0 for numbers, false for booleans, and an empty string for text.

That is why the program below prints sensible output even though active and title are never assigned.

package main import "fmt" func main() { var count int var active bool var title string fmt.Println(count, active, title + "empty") }

go run .

0 false empty

Naming rules

Names may contain letters, digits, and underscores, must start with a letter or an underscore, and are case sensitive, so age and Age are different variables.

Go convention uses camelCase for local variables, CamelCase for exported identifiers, and short names such as i or n for tiny scopes.

Reserved keywords cannot serve as names, and clear names beat short ones everywhere except loop counters.

Next: Constants

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: