Skip to main content
Master Go’s variable declaration syntax, type system, and type conversions through these hands-on exercises.

Basic Data Types

Get familiar with Go’s fundamental data types and literals.

Variable Declarations

Practice the standard variable declaration syntax with the var keyword.
Objective: Declare and use integer variablesDeclare int variables using the var keyword.Approach:
var age int
age = 25
fmt.Println(age)
Objective: Work with boolean variablesDeclare and initialize boolean variables.Approach:
var isActive bool
isActive = true
fmt.Println(isActive)
Objective: Handle floating-point numbersDeclare and use float64 variables for decimal numbers.Approach:
var price float64
price = 19.99
fmt.Println(price)
Objective: Work with text dataDeclare and manipulate string variables.Approach:
var name string
name = "Alice"
fmt.Println(name)
Objective: Understand what cannot be declared with varTry to declare variables with invalid type specifications.Key Concepts:
  • Valid vs invalid type names
  • Compiler error messages
  • Type system rules
Approach: Attempt declarations that will fail to understand the type system better.
Objective: Learn about sized integer typesUse specific integer types like int8, int16, int32, int64.Approach:
var small int8 = 127
var medium int16 = 32767
var large int64 = 9223372036854775807
Objective: Declare multiple variables efficientlyLearn different ways to declare multiple variables.Approach:
// Method 1
var x, y, z int

// Method 2
var (
    name string
    age  int
    active bool
)
Objective: Understand package scopeDeclare variables outside of functions at the package level.Key Concepts:
  • Package-level scope
  • Initialization order
  • Global state
Approach: Declare variables outside main() and access them within functions.

Short Declaration

Master the := operator for concise variable declarations.
Objective: Use the := operatorPractice the short declaration operator for local variables.Key Concepts:
  • Type inference
  • Local scope only
  • Simultaneous declaration and assignment
Approach:
name := "Bob"
age := 30
price := 19.99
Objective: Declare multiple variables with :=Declare and initialize multiple variables in one statement.Approach:
name, age, city := "Alice", 25, "NYC"
Objective: Use expressions in short declarationsAssign the result of expressions to variables using :=.Approach:
sum := 10 + 20
product := 5 * 4
length := len("Hello")
Objective: Use the blank identifierDiscard unwanted return values using _.Key Concepts:
  • Blank identifier _
  • Discarding values
  • Multiple return values
Approach:
_, err := someFunction()
value, _ := anotherFunction()
Objective: Understand redeclaration rulesLearn when you can redeclare variables with :=.Key Concepts:
  • At least one new variable required
  • Redeclaration vs reassignment
  • Scope rules
Approach:
x := 10
x, y := 20, 30  // OK: y is new
// x := 30      // Error: no new variables

Assignment and Type Conversion

Learn to work with variable assignments and convert between types.
Objective: Reassign variable valuesPractice assigning new values to existing variables.Approach:
color := "red"
color = "blue"  // Assignment (not declaration)
Objective: Use expressions in assignmentsAssign calculated values to variables.Approach:
x := 10
x = x + 5
x = x * 2
Objective: Apply variables to solve a problemCalculate and print the perimeter of a rectangle.Approach:
  1. Declare width and height
  2. Calculate perimeter: 2 * (width + height)
  3. Print the result
Objective: Assign to multiple variables simultaneouslyUse parallel assignment for multiple variables.Approach:
x, y := 1, 2
x, y = y, x  // Swap values
Objective: Swap variable valuesExchange the values of two variables.Approach:
a, b := 5, 10
a, b = b, a
fmt.Println(a, b)  // 10 5
Objective: Convert between numeric typesPractice explicit type conversion in Go.Key Concepts:
  • Explicit conversion required
  • No implicit conversions
  • Syntax: Type(value)
Approach:
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
Objective: Debug type mismatch issuesFix programs with type conversion errors.Approach:
  1. Identify type mismatches
  2. Add explicit conversions
  3. Ensure conversions are safe
Example fixes:
// Error: cannot use int as float64
var x float64 = 10  // Wrong

// Fixed:
var x float64 = float64(10)

Command-Line Arguments

Work with command-line input using os.Args.
Objective: Access command-line argumentsCount and print the number of arguments passed to your program.Key Concepts:
  • os.Args slice
  • Argument indexing
  • Program name in Args[0]
Approach:
import "os"

fmt.Println("Argument count:", len(os.Args))
Objective: Process multiple argumentsGreet each person passed as a command-line argument.Approach: Loop through os.Args[1:] and greet each person.

Next Steps

Control Flow

Learn conditionals and loops

Data Structures

Work with arrays, slices, and maps

Build docs developers (and LLMs) love