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
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
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
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
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
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
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
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
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
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
Run Code?
Click Run Button to view compiled output