Go Basics — Variables & Types

Purpose

Variables and types are the foundation of Go programs. Go is statically typed with type inference, keeping declarations concise while remaining explicit.

Architecture

Go infers types from values at compile time. Outside functions only var or const declarations are valid; inside functions the short declaration := is preferred.

Implementation Notes

Variable Declaration

// Short declaration (preferred inside functions)
name := "Alice"
age  := 30
 
// var style (required at package scope or when zero-value is needed)
var count int        // zero value = 0
var active bool      // zero value = false
var label string     // zero value = ""
 
// Multiple assignment
x, y := 10, 20

Constants

const pi = 3.14159
const greeting = "hello"
// Constants must be primitive types (string, int, float, bool)
// Cannot use := with const

Basic Types

TypeZero valueNotes
boolfalsetrue / false
string""UTF-8, immutable
int032 or 64-bit depending on platform
float640.0Standard decimal number
byte0Alias for uint8

Type Sizes

Use the standard sizes unless you have a specific memory constraint:

// Integers (signed)
int  int8  int16  int32  int64
 
// Unsigned integers
uint  uint8  uint16  uint32  uint64  uintptr
 
// Floats
float32  float64   // prefer float64
 
// Complex (rarely used)
complex64  complex128

The number suffix is the bit width. int and uint map to 32-bit or 64-bit depending on the environment. Prefer int, uint, float64 unless optimising memory layout.

Type Conversion

Go has no implicit conversions — cast explicitly:

temperatureFloat := 88.26
temperatureInt   := int64(temperatureFloat) // truncates decimal
 
var x int  = 42
var y float64 = float64(x)

Formatting Strings

// fmt.Sprintf returns a string; fmt.Printf prints to stdout
name := "Alice"
age  := 30
pi   := 3.14159
 
fmt.Sprintf("Hello, %s! You are %d years old.", name, age)
fmt.Sprintf("Pi is approximately %.2f", pi)  // 2 decimal places
fmt.Sprintf("Value: %v, Type: %T", age, age) // %v default, %T type name
VerbMeaning
%vDefault representation
%TType name
%dInteger (decimal)
%fFloat (%.2f = 2 decimals)
%sString
%tBoolean

Trade-offs

  • := is unavailable at package scope — use var there.
  • Casting float → int truncates (does not round).
  • Unused variables cause a compile error; use _ to discard.
  • Go has no implicit type coercion, so mixed arithmetic requires explicit casts.

References