HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›Variables

Variables

The := short declaration, explicit type with var, const constants, and the "unused variable" rule.

Simple enough for a 5-year-old to understand

:= and var

Example code
1
package main
2
import "fmt"
3
func main() {
4
name := "Alex"
Short declaration := (type is automatic).
VariableShort declaration := (type is automatic).
5
var age int = 36
Explicit type with var.
VariableExplicit type with var.
6
fmt.Println(name, age)
Alex 36
Output / InputAlex 36
7
}
VariableOutput / Input
💡 Hint: In Go, a variable you define and DO NOT use is an ERROR — do not leave redundant variables.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Define and print city := "Konya".

🎯 Expected output: Konya

Loading editor…

About this topic

There are two ways to declare variables in Go. The long form explicitly writes the type using the var keyword; the short form infers the type from the value using colon-equals. Inside functions, the short form is almost always used.

An important feature of Go is ZERO VALUES: every variable you define gets a usable initial value. Numbers are zero, strings are empty, booleans are false, pointers and slices are nil. That's why Go doesn't have an "uninitialized variable" error class.

Constants use const and their values must be known at compile time. Constants in Go can be untyped — this is a subtle but useful feature that lets you use a constant in both int and float contexts.

What you will learn

  • var and short declaration forms
  • The concept of zero values
  • Multiple assignment and value swapping
  • defining constants with const
  • Discarding values with the underscore
  • Scope rules

When to use?

The short form only works INSIDE functions; at the package level, you must use var. Also, the short form must define at least ONE NEW variable, otherwise you get a compilation error.

The underscore is used to discard a return value you don't want. Since Go functions frequently return two values — a result and an error — this is very common. But discarding the error is almost always wrong.

Common mistakes

Wrong

package main

x := 5

func main() {
	println(x)
}

The short definition form is only valid inside functions; at the package level, you get a "syntax error: non-declaration statement outside function body" error. var must be used at the package level.

Correct

package main

var x = 5

func main() {
	println(x)
}

Wrong

a := 1
a := 2

The short form must define at least one NEW variable; reusing an existing variable gives a "no new variables on left side of :=" error. Plain equals is used to assign values.

Correct

a := 1
a = 2

Wrong

var total int
total = "on"

Go is strongly typed and DOES NOT do implicit conversion; assigning a string to a number variable gives a "cannot use "on" (untyped string constant) as int value" error. Go does not automatically convert even between int and int64.

Correct

var total int
total = 10

Frequently asked questions

Should I use var or the short form?

Short form inside functions — it's shorter and that's the Go idiom. var is only used at the package level, when you want to initialize with a zero value, or when you need to write the type differently from the value.

What are zero values good for?

They guarantee that every variable has a usable initial value, so the "uninitialized variable" error does not exist in Go. They also let you use structs without explicitly populating them — a counter struct works directly with its zero value.

How do I convert between types?

Explicitly, by calling the target type like a function. Go doesn't do implicit conversion even between int and int64; this strictness may seem annoying, but it prevents overflows and precision losses from happening silently.

Summary

Inside functions in Go, short definition is used, and at the package level, var is used. Every variable gets a zero value, there is no implicit type conversion at all, and the short form must define at least one new variable.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code