Variables and Types in Go
Learn about variables, data types, type inference, and constants in Go with practical Docker-ready examples
Go takes a pragmatic approach to variables and types. It is statically and strongly typed, meaning every variable has a fixed type determined at compile time, but Go’s type inference often lets you skip writing types explicitly. The result is code that feels nearly as concise as a dynamically typed language while retaining the safety guarantees of a static type system.
Understanding Go’s type system is essential because the compiler catches type mismatches before your code ever runs. Go has no implicit type conversions between numeric types — you must be explicit, which eliminates an entire class of subtle bugs common in C and C++.
In this tutorial you will learn how to declare variables using both explicit types and short declaration syntax, work with Go’s basic types, convert between types, and define constants.
Variable Declaration
Go offers several ways to declare variables. The most common in practice is the short declaration operator :=, which infers the type from the value on the right-hand side. You can also use the var keyword for explicit declarations, which is required at the package level or when you want a variable initialized to its zero value.
Create a file named variables.go:
| |
Every type in Go has a zero value — the default when no initial value is provided. Numeric types default to 0, booleans to false, and strings to the empty string "". This means variables in Go are always initialized, eliminating the undefined-variable bugs found in some other languages.
Basic Types and Conversions
Go has a rich set of numeric types with explicit sizes. Unlike C, where int varies by platform, Go’s sized types (int8, int32, int64) have guaranteed widths. The plain int type is platform-dependent (32 or 64 bits) but is the idiomatic choice for most integer work.
Go requires explicit type conversions — there is no automatic promotion between types. This is a deliberate design choice that prevents subtle data loss.
Create a file named variables_types.go:
| |
Constants
Go distinguishes between variables and constants with the const keyword. Constants are determined at compile time and cannot be changed. Go also supports untyped constants, which have higher precision and can be used more flexibly across expressions involving different numeric types.
The iota enumerator is a powerful feature for creating sequences of related constants, commonly used to define enumerations.
Create a file named variables_constants.go:
| |
Running with Docker
| |
Expected Output
Output from variables.go:
=== Explicit Declarations ===
age: 30
name: Go Developer
pi: 3.14159
active: true
=== Short Declarations ===
city: Chicago
year: 2009
ratio: 0.618
verbose: false
=== Zero Values ===
int: 0, float64: 0, string: "", bool: false
=== Multiple Assignment ===
a, b, c: 1 2 3
x, y: hello 42
=== Package Level ===
I'm accessible throughout the package
Output from variables_types.go:
=== Integer Types ===
int8: 127
int32: 2147483647
int64: 9223372036854775807
uint8: 255
=== Float Types ===
float32: 3.14
float64: 3.141592653589793
=== Strings and Runes ===
greeting: Hello, 世界
letter: A (code point: 65)
byte: A (value: 65)
greeting length in bytes: 13
=== Type Conversions ===
int to float64: 42
float64 to int (42.9): 42
int 72 as rune: H
=== Type Inspection ===
intVal: int, floatVal: float64, greeting: string
Output from variables_constants.go:
=== Constants ===
Pi: 3.141592653589793
App: CodeArchaeology
=== Days (iota) ===
Sunday: 0
Wednesday: 3
Saturday: 6
=== Permissions (bit flags) ===
Read: 1
Write: 2
Execute: 4
Read + Write: 3
=== Untyped Constants ===
tiny: 10
Key Concepts
- Short declarations (
:=) infer the type from the value and are the most common way to declare variables inside functions - Zero values guarantee every variable is initialized —
0for numbers,falsefor bools,""for strings - No implicit type conversions — Go requires explicit casts like
float64(x), preventing silent data loss runeis an alias forint32and represents a Unicode code point;byteis an alias foruint8- Sized integer types (
int8,int16,int32,int64) have guaranteed widths, unlike C’s platform-dependent sizes constwithiotaprovides a clean way to define enumerations and bit flags without magic numbers- Untyped constants have higher precision than any concrete type and adapt to their usage context
varis required at the package level and when you want zero-value initialization without an explicit value
Comments
Loading comments...
Leave a Comment