Go Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
Go Introduction
Exercise
Which package prints text?
import ""
Go Get Started
Exercise
Run a Go file from the terminal.
go hello.go
Go Syntax and Comments
Exercise
Start a single line comment.
this is a commentVariables and Types
Exercise
Short declaration operator.
x 5
If, For and Switch
Exercise
Which keyword makes a loop?
i := 0; i < 3; i++ {}Declare Variables
Exercise
Declare and assign inside a function.
n 10
Variable Naming Rules
Exercise
Which kind of name is exported?
A name that starts with a letter
Constants
Exercise
Declare a constant.
Max = 10Go Output
Exercise
Verb for an integer.
fmt.Printf("%", 5)
Declare Multiple Variables
Exercise
Ignore a value.
, n := 1, 2Zero Values and new
Exercise
Zero value of a pointer.
var p *int // p == Variables
Exercise
Short declaration.
x 5
Constants and iota
Exercise
Constant keyword.
Max = 10Basic Types
Exercise
Decimal type.
var f = 2.5
Type Conversion
Exercise
Convert to float.
(i)Printf Formatting
Exercise
Print the type.
fmt.Printf("%", x)
Println, Sprintf and Errorf
Exercise
Return a formatted string.
s := fmt.("%d", 1)
Reading Input
Exercise
Read from a reader.
fmt.(r, &n)
Arithmetic and Operators
Exercise
Remainder.
7 2
The math Package
Exercise
Square root.
math.(16)
The strings Package
Exercise
Join parts.
strings.(parts, "-")
Strings, Bytes and Runes
Exercise
Count characters.
len([](s))
Booleans and Logic
Exercise
Logical not.
aScope and Shadowing
Exercise
Block scope.
Variables live in a Comparing Values
Exercise
Compare slices.
slices.(a, b)
min, max and clear
Exercise
Smallest.
(3, 1, 2)The slices Package
Exercise
Test presence.
slices.(a, 2)
The maps Package
Exercise
Copy a map.
maps.(m)
Data Types
Exercise
Convert i to a float.
f := (i)
Strings
Exercise
Make text upper case.
strings.(s)
Numbers and strconv
Exercise
Turn text into an int.
n, err := strconv.("12")
Booleans, Integers and Floats
Exercise
Larger of two numbers.
math.(3, 9)
Runes, Bytes and Unicode
Exercise
Type for a Unicode character.
var r = 'a'
Custom Types
Exercise
Declare a new type.
Celsius float64Operators
Exercise
Remainder operator.
fmt.Println(17 5)
Bitwise Operators
Exercise
Test a flag.
p Write != 0
Comparison and Logical Operators
Exercise
Logical not.
(a > b)If, Else If and Nested If
Exercise
Add a second check.
if a {} if b {}
Switch
Exercise
Run when no case matches.
switch x { : }
For Loops
Exercise
Skip to the next round.
if i == 2 { }
Labels, break and continue
Exercise
Leave an outer loop.
outerType Switch and fallthrough
Exercise
Check the dynamic type.
switch v := x.() {
if and else
Exercise
Else branch.
if x {} {}
for Is the Only Loop
Exercise
While style loop.
for > 0 {}
range
Exercise
Ignore the index.
for , v := range a
switch
Exercise
Force fall through.
break, continue and Labels
Exercise
Skip a round.
defer
Exercise
Run at exit.
fmt.Println("x")Simple Programs: Sum and Factorial
Exercise
Accumulate.
sum i
Arrays, Slices and Maps
Exercise
Grow a slice.
nums = (nums, 4)
Structs and Methods
Exercise
Declare a struct.
type Point { X int }
Arrays
Exercise
Declare an array of 5 ints.
var a int
Slices
Exercise
Create a slice with make.
s := ([]int, 3)
Maps
Exercise
Remove a key.
(m, "a")Slices In Depth
Exercise
Sort a slice.
slices.(s)
Maps In Depth
Exercise
Create a map.
m := (map[string]int)
Sorting
Exercise
Sort integers.
sort.(n)
strings.Builder and Helpers
Exercise
Efficient string builder type.
var sb strings.Arrays
Exercise
Array length.
len()
Slices
Exercise
Add items.
s = (s, 4)
make and Capacity
Exercise
Create a slice.
([]int, 3)copy and Sharing
Exercise
Copy slices.
(dst, src)Slices of Slices
Exercise
Grid type.
[]int
Maps
Exercise
Test for a key.
v, := m["k"]
Iterating Maps in Order
Exercise
Sort strings.
sort.(keys)
Structs
Exercise
Define a struct.
type P {}
Pointers
Exercise
Address of.
p := x
Methods
Exercise
Receiver.
func (r ) Grow() {}
Struct Embedding
Exercise
Reuse fields.
type Dog struct { }
Interfaces
Exercise
Interface keyword.
type S { Area() float64 }
Type Assertions and Switches
Exercise
Type switch.
switch x := v.()
Generics
Exercise
Type parameter list.
func F(x T) {}
Functions
Exercise
Declare a function.
add(a, b int) int { return a + b }Errors
Exercise
Check for a failure.
if err nil {}
Interfaces
Exercise
Declare an interface.
type S { Speak() string }
Parameters, Returns and Recursion
Exercise
A function that calls itself uses...
Defer, Panic and Recover
Exercise
Run a call when the function ends.
file.Close()Closures and Function Values
Exercise
Declare a function literal.
f := (x int) int { return x }
Variadic Functions and Method Values
Exercise
Pass a slice as arguments.
sum(nums)
Packages, init and Imports
Exercise
Start a module.
go mod example.com/app
Errors In Practice
Exercise
Wrap with context verb.
fmt.Errorf("x: ", err)
Functions
Exercise
Return two values.
func f() (int, )
Named Returns
Exercise
Bare return.
Variadic Functions
Exercise
Variadic syntax.
func f(n int)
Function Values and Closures
Exercise
Return a function.
func counter() int
Errors
Exercise
Check the error.
if err nil
Custom Errors and Wrapping
Exercise
Wrap an error.
fmt.Errorf("x: %", err)
panic and recover
Exercise
Catch a panic.
()Packages, main and init
Exercise
Entry function.
func () {}
Testing Basics
Exercise
Test file suffix.
file.go
The time Package
Exercise
Reference layout year.
Format("-01-02")
JSON
Exercise
Struct tag.
`json:""`
Files with os
Exercise
Read a file.
os.(p)
bufio Scanner
Exercise
Read each line.
for sc.() {}
The sort Package
Exercise
Sort by rule.
sort.(p, less)
strings.Builder
Exercise
Write text.
b.("x")
Practice: Word Count
Exercise
Split on spaces.
strings.(text)
Practice: Fibonacci
Exercise
Swap in one line.
a, b = b, ab
Practice: Reverse and Palindrome
Exercise
Runes.
[](s)
Goroutines and Channels
Premium exercise. See plans
Buffered Channels and range
Premium exercise. See plans
Pointers
Premium exercise. See plans
Struct Tags, Embedding and Composition
Premium exercise. See plans
Generics
Premium exercise. See plans
Custom Errors and Wrapping
Premium exercise. See plans
WaitGroups and Mutexes
Premium exercise. See plans
Select, Timeouts and Worker Pools
Premium exercise. See plans
Context and Cancellation
Premium exercise. See plans
JSON with encoding/json
Premium exercise. See plans
Files and io
Premium exercise. See plans
Embedding Interfaces and Method Sets
Premium exercise. See plans
sync/atomic, Once and sync.Pool
Premium exercise. See plans
Testing with go test
Premium exercise. See plans
Building an HTTP Server
Premium exercise. See plans
Benchmarks and Profiling
Premium exercise. See plans
Modules and Tooling
Premium exercise. See plans
Databases with database/sql
Premium exercise. See plans
REST APIs with net/http and Frameworks
Premium exercise. See plans
Docker and Deployment
Premium exercise. See plans
Go Best Practices
Premium exercise. See plans