30 Golang Basic Exercises for Intermediate with Solutions

Master intermediate Golang skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Golang, setting a solid foundation for advanced challenges. Start your journey to Golang mastery today!

Learning Objectives:

Enhance your skills with concurrency (goroutines, channels), error handling, struct types, interfaces, and working with packages.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Create a Go program to write data to a file and then read it back.

Required Input:

A string Hello, Go! to write into a file named output.txt.

Expected Output:

Hello, Go!

Code In Go

package main import "fmt" func main() { // Write code to write data to a file and read it back }

Run Code?

Click Run Button to view compiled output

2. Write a Go program to demonstrate the use of a buffered channel with make.

Required Input:

A buffered channel with capacity 3.

Expected Output:

Message 1
Message 2
Message 3

Code In Go

package main import "fmt" func main() { // Demonstrate the use of a buffered channel }

Run Code?

Click Run Button to view compiled output

3. Create a Go program to implement a goroutine that calculates the factorial of a number.

Required Input:

An integer n = 5.

Expected Output:

Factorial: 120

Code In Go

package main import "fmt" func factorial(n int, result chan int) { // Implement the factorial calculation } func main() { // Launch a goroutine to calculate the factorial }

Run Code?

Click Run Button to view compiled output

4. Write a Go program to launch multiple goroutines and synchronize them using a WaitGroup.

Required Input:

Three goroutines printing messages.

Expected Output:

Goroutine 3
Goroutine 2
Goroutine 1
All goroutines finished.

Code In Go

package main import ("fmt" "sync") func printMessage(message string, wg *sync.WaitGroup) { // Implement the goroutine functionality } func main() { // Launch and synchronize goroutines using WaitGroup }

Run Code?

Click Run Button to view compiled output

5. Create a Go program to demonstrate the use of an interface to handle multiple struct types.

Required Input:

Two structs Dog and Cat implementing a method Speak.

Expected Output:

Woof!
Meow!

Code In Go

package main import "fmt" type Animal interface { Speak() string } func printSpeak(animal Animal) { // Print the message from the Speak method } func main() { // Implement the interface and call the Speak method for different types }

Run Code?

Click Run Button to view compiled output

6. Write a Go program to implement a custom error using the errors.New() function.

Required Input:

A predefined condition that triggers the error.

Expected Output:

Error: value cannot be negative

Code In Go

package main import ("errors" "fmt") func checkValue(value int) error { // Return an error if the value is less than 0 return nil } func main() { // Check the value and handle the error }

Run Code?

Click Run Button to view compiled output

7. Create a Go program to demonstrate the use of type assertion with an empty interface.

Required Input:

An empty interface holding a value (e.g., "Hello, Go!").

Expected Output:

Type: string, Value: Hello, Go!

Code In Go

package main import "fmt" func main() { // Demonstrate type assertion with an empty interface }

Run Code?

Click Run Button to view compiled output

8. Write a Go program to sort a slice of integers in descending order.

Required Input:

A slice of integers (e.g., [5, 2, 8, 1, 9]).

Expected Output:

[9 8 5 2 1]

Code In Go

package main import "fmt" func main() { // Sort a slice of integers in descending order }

Run Code?

Click Run Button to view compiled output

9. Create a Go program to demonstrate the use of select with channels.

Required Input:

Two channels sending different messages.

Expected Output:

Message from channel 1
Message from channel 2

Code In Go

package main import "fmt" func main() { // Use select to handle messages from multiple channels }

Run Code?

Click Run Button to view compiled output

10. Write a Go program to find duplicate elements in a slice of integers.

Required Input:

A slice of integers (e.g., [1, 2, 3, 2, 4, 3, 5]).

Expected Output:

Duplicates: [2 3]

Code In Go

package main import "fmt" func main() { // Find and print duplicate elements in a slice }

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3