0 day streak
Code library

Beginner Go exercises

Read these Beginner Go snippets line by line — each one comes with a written breakdown of what the code does and why.

Go · Beginner

Variable Declarations

In Go, you can declare variables using the 'var' keyword with an explicit type, or the short declaration operator ':=' which infers the type automatically.

SyntaxData Types
Open snippet →
Go · Beginner

Working with Slices

Slices are dynamic windows into arrays. Unlike arrays, slices can grow in size using the built-in 'append' function.

Arrays & ListsData Types
Open snippet →
Go · Beginner

The For Loop

Go only has one looping construct: 'for'. It can be used as a standard C-style loop or as a 'while' loop by omitting the initialization and post statements.

Control FlowSyntax
Open snippet →
Go · Beginner

Error Handling Pattern

Instead of using exceptions, Go uses explicit error checking. Functions return an 'error' type as their last return value, which should be checked immediately.

Error HandlingBest Practices
Open snippet →
Go · Beginner

Basic Arrays

Arrays in Go have a fixed length that is part of their type. Once defined, their size cannot change.

Arrays & ListsData Types
Open snippet →
Go · Beginner

Defining Structs

Structs are used to group related data together. Unlike classes in other languages, Go uses structs to define the shape of data objects.

Data TypesSyntax
Open snippet →
Go · Beginner

Methods on Types

A method is a function with a special receiver argument. It allows you to attach behavior to structs.

FunctionsSyntax
Open snippet →
Go · Beginner

Variable Declaration and Type Inference

Go allows two ways to declare variables. The `var` keyword with explicit type, or the `:=` short syntax which infers the type from the value. Variables declared with `:=` inside a function are call…

SyntaxData Types
Open snippet →
Go · Beginner

Working with Slices

Slices are dynamic, growable sequences in Go. Unlike arrays, slices can change size. The `append` function adds elements and automatically grows the underlying array when needed. `len()` returns th…

Data TypesArrays & Lists
Open snippet →
Go · Beginner

Error Handling with Multiple Return Values

Go uses multiple return values for error handling, a core design pattern. Functions return both a value and an error. The error is nil on success, otherwise it contains the error message. The `if e…

Error HandlingFunctions
Open snippet →
Go · Beginner

For Loop as While Alternative

Go has only one loop keyword: `for`. There is no `while` keyword. The `for` loop without initialization and post statements acts as a while loop. This makes Go simpler while remaining powerful.

Control FlowSyntax
Open snippet →
Go · Beginner

Range Iteration Over Slices

The `range` keyword iterates over slices and maps. It returns two values: index and element. The blank identifier `_` is used when you don't need a value, telling Go to discard it.

Control FlowArrays & Lists
Open snippet →
Go · Beginner

Understanding Go Maps

Maps in Go are unordered key-value data structures. They are created using the built-in `map` keyword with syntax `map[KeyType]ValueType`. You can add entries using bracket notation, check for exis…

Data TypesSyntax
Open snippet →
Go · Beginner

Defining Custom Types with Structs

Structs are composite types that group together related fields. You define a struct using the `type` keyword, then create instances with brace initialization. Methods can be attached to structs usi…

Data TypesFunctions
Open snippet →
Go · Beginner

Pointers and Memory Addresses

Pointers hold memory addresses instead of values. The `&` operator gets a pointer to a variable, while `*` dereferences a pointer to access the value. In `double`, the `*int` parameter expects a po…

SyntaxData Types
Open snippet →
Go · Beginner

Switch Statements in Go

Go's switch is more flexible than in many languages. It can compare against values or conditions without an expression. Cases don't fall through by default (no break needed), but you can use `fallt…

Control FlowSyntax
Open snippet →
Go · Beginner

Clean-up with Defer

The `defer` keyword schedules a function call to execute when the surrounding function returns. This is essential for cleanup tasks like closing files or releasing resources. Deferred functions exe…

FunctionsBest Practices
Open snippet →
Go · Beginner

Constants with iota for Enumerated Values

The `iota` keyword generates consecutive untyped integer constants. In the first block, it creates color codes (1, 2, 3). In the second block, `1 << iota` creates bit flags (1, 2, 4). The underscor…

SyntaxBest Practices
Open snippet →
Go · Beginner

Variadic Functions for Flexible Arguments

Variadic functions accept zero or more arguments using the `...T` syntax. Inside the function, the variable becomes a slice. You can also have required parameters before the variadic part. This pat…

FunctionsSyntax
Open snippet →
Go · Beginner

String Indexing and Slicing

Go strings are byte arrays, not rune arrays. `len(s)` returns byte count, which differs for Unicode. Use `len([]rune(s))` for actual character count. Slice bytes directly with `s[:5]` but use runes…

Data TypesSyntax
Open snippet →
Go · Beginner

Type Assertions and Type Switches

Empty interface `interface{}` accepts any type. Type assertions extract the underlying type with `i.(type)` in switch or `i.(T)` for direct assertion. The comma-ok idiom checks if assertion succeed…

Control FlowData Types
Open snippet →
Go · Beginner

Packages and Importing Modules

Go uses packages to organize code into modular units. The import statement brings in external packages from the standard library or third-party sources. Packages like fmt for formatted I/O, math fo…

SyntaxBest Practices
Open snippet →
Go · Beginner

Basic Data Types and Type Conversion

Go is a statically typed language with explicit type declarations. The basic types include int for integers, float64 for decimal numbers, bool for true/false values, and string for text. Go does no…

Data TypesSyntax
Open snippet →
Go · Beginner

If Statement with Comparison Operators

If statements in Go evaluate boolean conditions and execute code blocks accordingly. Unlike many languages, Go does not use parentheses around conditions. The else if chain allows for multiple cond…

Control FlowData Types
Open snippet →
Go · Beginner

Introduction to Goroutines for Concurrency

Goroutines are lightweight threads managed by the Go runtime, enabling concurrent execution. Simply prefix a function call with 'go' to run it concurrently. Unlike parallel processing (using multip…

Async & ConcurrencyFunctions
Open snippet →
Go · Beginner

Writing Basic Tests in Go

Go has built-in testing support through the testing package. Test files are named with _test.go suffix. Test functions start with Test prefix and receive a *testing.T argument. The t.Errorf() metho…

TestingFunctionsBest Practices
Open snippet →
Go · Beginner

Slices: Dynamic Arrays in Go

Slices are growable dynamic views into arrays. Unlike arrays with fixed size, slices can grow or shrink using append(). The make() function creates a slice with pre-allocated capacity. Slicing with…

Data TypesArrays & Lists
Open snippet →
Go · Beginner

The Versatile for Loop

Go has only one loop construct: for. It can operate as a classic three-part for loop, a while-style loop, or an infinite loop with break. The range clause iterates over slices, arrays, maps, and st…

Control FlowSyntax
Open snippet →