Go Exercises

Golang exercises for beginner programmers. If you are looking for a Go challenge and are a beginner programmer, this might be for you. These exercises will help you with Go training.

You can download the answers here: Download answers

Go Practice

Beginner exercises

Hello world

  • Create a program that shows your name
  • Create a program that shows your address

Comments

  • Create a program and add a comment with your name

Strings

  • Create a program with multiple string variables
  • Create a program that holds your name in a string.

Keyboard input

  • Make a program that lets the user input a name
  • Get a number from the console and check if it’s between 1 and 10.

Variables

  • Calculate the year given the date of birth and age
  • Create a program that calculates the average weight of 5 people.

Scope

  • What’s the difference between a local and global variable?
  • How can you make a global variable?

Arrays

  • Create an array with the number 0 to 10
  • Create an array of strings with names

For loops

  • Can for loops exist inside for loops?
  • Make a program that counts from 1 to 10.

Range

  • What is the purpose of range ?
  • What the difference between the line for index, element := range a and the line for _, element := range a ?

If statements

  • Make a program that divides x by 2 if it’s greater than 0
  • Find out if if-statements can be used inside if-statements.

While loops

  • How does a while loop differ from a for loop?

Files

File exists

  • Check if a file exists on your local disk
  • Can you check if a file exists on an external disk?

Read file

  • Think of when you’d read a file ‘line by line’ vs ‘at once’?
  • Create a new file containing names and read it into an array

Write file

  • Write a list of cities to a new file.

Rename file

  • Which package has the rename function?

Intermediate exercises

Struct

  • Create a struct house with variables noRooms, price and city
  • How does a struct differ from a class?

Maps

  • What is a map?
  • Is a map ordered?
  • What can you use a map for?

Random numbers

  • Make a program that rolls a dice (1 to 6)
  • Can you generate negative numbers?

Pointers

  • Where are variables stored in the computer?
  • What is a pointer?
  • How can you declare a pointer?

Slices

  • Take the string ‘hello world’ and slice it in two.
  • Can you take a slice of a slice?

Functions

Methods

  • Create a method that sums two numbers
  • Create a method that calls another method.

Defer

  • Predict what this code does:
defer fmt.Println("Hello")  
defer fmt.Println("!")  
fmt.Println("World")  

Multiple return

  • Change the return values from 2,4 to “hello”,”world”. Does it still work?
  • Can a combination of strings and numbers be used?

Variadic functions

  • Create a variadic function that prints the names of students

Recursion

  • When is a function recursive?
  • Can a recursive function call non-recursive functions?

Goroutines and channels

Goroutines

  • What is a goroutine?
  • How can you turn a function into a goroutine?
  • What is concurrency?

Channels

  • When do you need channels?

  • How can you send data into a channel?

  • How can you read data from a channel?

  • Go Practice

  • Beginner exercises

  • Files

  • Intermediate exercises

  • Functions

  • Goroutines and channels