In this lesson we cover the Go variables tutorial essentials — how to declare variables, choose types, work with constants, and print formatted output. If you completed Lesson 3 – Go Basic Syntax, you already know the skeleton of a Go program. Here we add the data layer: names, types, and values that make programs useful.
Prerequisites: Go 1.22 or newer installed (go version in your terminal) and familiarity with Go Programming – Setup and Go Basic Syntax. Estimated time: 35–45 minutes.
1. Declaring Variables with var
The var keyword declares a variable and optionally assigns an initial value. You can declare one variable at a time or group several in a single block:
package main
import "fmt"
func main() {
var name string
name = "Kindson"
var age int = 30
var city = "Lagos" // type inferred as string
var (
language = "Go"
year = 2026
)
fmt.Println(name, age, city, language, year)
}
When you omit the initial value, Go assigns the zero value for that type (covered later). When you omit the type but provide a value, Go infers the type from the literal. Use var at package level or when you want an explicit type that inference might not capture cleanly.
2. Short Variable Declaration with :=
Inside functions, the := operator is the most common way to declare and initialize a variable in one step:
package main
import "fmt"
func main() {
message := "Hello from Go"
count := 42
pi := 3.14159
isReady := true
fmt.Println(message, count, pi, isReady)
}
Important rules for :=:
- It can only be used inside functions — not at package level.
- At least one variable on the left must be new in the current scope. You can reuse
:=if one name is new:count, err := doWork(). - Go always infers the type; you cannot write
x := intwithout a value.
If you are coming from Python, think of := as similar to a first assignment that also creates the name — but Go is statically typed, so the type is fixed at declaration.
3. Constants
Constants are values that cannot change after compilation. Declare them with const:
package main
import "fmt"
const AppName = "KindsonGo"
const MaxUsers = 1000
const (
StatusOK = 200
StatusError = 500
)
func main() {
fmt.Println(AppName, MaxUsers, StatusOK)
}
Constants must be compile-time values — numbers, strings, or boolean literals. You cannot assign the result of a function call to a const. For configuration that never changes (API version, buffer size, status codes), constants keep intent clear and prevent accidental reassignment.
4. Basic Types in Go
Go provides a small set of built-in types. Understanding them early prevents bugs when you mix integers, floats, and strings:
| Category | Type | Example | Notes |
|---|---|---|---|
| Boolean | bool |
true, false |
Used in conditions and logic |
| String | string |
"hello" |
UTF-8 text; immutable |
| Integer | int, int8…int64 |
42, -7 |
int size depends on platform (32 or 64 bit) |
| Unsigned | uint, byte (uint8) |
255 |
Non-negative only |
| Floating | float32, float64 |
3.14 |
Default untyped float is float64 |
| Complex | complex64, complex128 |
1+2i |
Rare in everyday code |
| Rune | rune (int32) |
'A', '世' |
Represents a Unicode code point |
Start with int, float64, string, and bool for most programs. Reach for sized types (int32, uint8) when you care about memory layout or interoperability with other systems.
5. Zero Values
Every type in Go has a default value when you declare a variable without initializing it:
0for numeric typesfalseforbool""(empty string) forstringnilfor pointers, slices, maps, channels, interfaces, and function types
package main
import "fmt"
func main() {
var count int
var name string
var active bool
fmt.Printf("count=%d, name=%q, active=%t\n", count, name, active)
// Output: count=0, name="", active=false
}
Zero values mean Go never leaves variables in an undefined state — a safety feature compared to languages where uninitialized variables contain garbage. You will rely on zero values often when declaring structs and slices in later lessons.
6. Type Conversion
Go does not perform implicit numeric conversions. You must convert explicitly:
package main
import "fmt"
func main() {
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
var x int32 = 10
var y int64 = int64(x) + 50
fmt.Println(i, f, u, y)
}
Syntax is Type(value) — for example int64(x) or string(65) for rune-to-string (not for arbitrary int-to-string; use strconv for that). Mixing int and float64 without conversion is a compile error, which catches precision bugs early.
7. Formatted Output with fmt.Printf
fmt.Println prints values with spaces; fmt.Printf gives you control over format using verbs:
package main
import "fmt"
func main() {
name := "Ada"
score := 98.6
passed := true
fmt.Printf("Name: %s\n", name)
fmt.Printf("Score: %.1f\n", score)
fmt.Printf("Passed: %t\n", passed)
fmt.Printf("Type of score: %T\n", score)
}
Common verbs: %s string, %d integer, %f float, %t boolean, %v default format, %T type, %q quoted string. Add \n for a newline — Printf does not add one automatically. For quick debugging, fmt.Printf("%+v\n", someValue) is handy with structs later.
8. Next Steps
You now understand Go variables and types: var, :=, constants, basic types, zero values, type conversion, and fmt.Printf. In the next lesson we cover control flow — if, switch, and for loops that branch and repeat logic based on your data.
Continue the series: Lesson 1 – Introduction · Lesson 2 – Setup · Lesson 3 – Basic Syntax · Lesson 5 – Control Flow
Browse all tutorials: Content Directory · Go Programming Hub
Frequently Asked Questions
What is the difference between var and := in Go?
var works at package level and inside functions; you can declare without a value. := only works inside functions and requires an initial value while inferring the type.
Does Go have implicit type conversion?
No. You must convert between types explicitly with syntax like float64(myInt). This prevents silent precision loss and unexpected behavior.
What are zero values in Go?
Zero values are defaults assigned when you declare a variable without initialization: 0 for numbers, false for booleans, "" for strings, and nil for reference types.
Want live Go or backend classes? Join Alkademy for instructor-led programming courses with hands-on projects.
[…] Previous: Lesson 4: Go Variables and Types – Storing Data in Go […]