
Practice 50 Go Lang Advanced Topics Coding Questions
Q1. Write a Go program that uses Goroutines to calculate the sum of an array concurrently.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Sum: 15
Q2. Write a Go program that demonstrates how to use a WaitGroup to wait for multiple Goroutines to finish.
Input:
Start 3 Goroutines, each sleeping for 1 second
Expected Output:
All Goroutines have completed
Q3. Write a Go program that demonstrates the use of Channels for communication between Goroutines.
Input:
Goroutine sends "Hello" over the channel
Expected Output:
Received: Hello
Q4. Write a Go program that uses a select statement to listen for multiple channels.
Input:
Channel 1 sends "First" and Channel 2 sends "Second"
Expected Output:
Received: First
Q5. Write a Go program that demonstrates the use of Mutex for mutual exclusion in concurrent code.
Input:
Two Goroutines attempt to increment a counter simultaneously
Expected Output:
Final Counter: 2
Q6. Write a Go program that demonstrates the use of the `defer` keyword for closing a file.
Input:
Open a file and defer closing it after writing data
Expected Output:
File closed after writing
Q7. Write a Go program that demonstrates how to use the `time.Sleep` function to simulate delay in Goroutines.
Input:
Goroutine sleeps for 2 seconds before printing "Done"
Expected Output:
Done (after 2 seconds)
Q8. Write a Go program that demonstrates how to use the `sync.Once` type to ensure that a function is only executed once.
Input:
Execute a function multiple times using `Once`
Expected Output:
Function executed only once
Q9. Write a Go program that demonstrates how to handle panics using `recover`.
Input:
Invoke a panic and use recover to handle it
Expected Output:
Panic caught: Something went wrong
Q10. Write a Go program that demonstrates how to pass a function as an argument (first-class functions).
Input:
Pass a function that multiplies two numbers to another function
Expected Output:
Result: 20
Q11. Write a Go program that uses Go's `context` package to manage cancellation of Goroutines.
Input:
Run a Goroutine that can be canceled after 1 second using context
Expected Output:
Goroutine canceled after 1 second
Q12. Write a Go program that demonstrates how to use the `reflect` package to inspect the type of a variable.
Input:
Use reflect to inspect the type of an integer variable
Expected Output:
Type: int
Q13. Write a Go program that demonstrates the use of `interface{}` to handle unknown types.
Input:
Pass a string and an integer to a function that accepts `interface{}`
Expected Output:
String: Hello, Integer: 5
Q14. Write a Go program that demonstrates how to implement and use custom error types.
Input:
Call a function that returns a custom error
Expected Output:
Error: File not found
Q15. Write a Go program that demonstrates how to implement an interface.
Input:
Implement an interface for a shape with methods `Area` and `Perimeter`
Expected Output:
Area: 25, Perimeter: 20
Q16. Write a Go program that uses `defer` to log function execution times.
Input:
Measure the time taken to execute a function
Expected Output:
Function executed in: X seconds
Q17. Write a Go program that demonstrates how to use the `time.After` function for delaying an action.
Input:
Wait for 2 seconds before printing a message
Expected Output:
Done after 2 seconds
Q18. Write a Go program that demonstrates how to create and use Go channels with buffered capacity.
Input:
Send and receive messages over a buffered channel
Expected Output:
Message received: Hello
Q19. Write a Go program that uses a Goroutine to execute a task asynchronously and returns the result via a channel.
Input:
Goroutine calculates the square of 4
Expected Output:
Square: 16
Q20. Write a Go program that demonstrates how to use an anonymous function.
Input:
Define and execute an anonymous function that calculates the sum of 5 and 6
Expected Output:
Sum: 11
Q21. Write a Go program that shows how to use Go's `map` type for creating key-value pairs.
Input:
Create a map with keys "a" and "b" and values 1 and 2
Expected Output:
a: 1, b: 2
Q22. Write a Go program that shows how to implement a custom iterator using channels.
Input:
Iterate over a list of numbers using a custom iterator
Expected Output:
1, 2, 3, 4, 5
Q23. Write a Go program that uses a Goroutine to handle file processing.
Input:
Goroutine reads a file and processes its content
Expected Output:
File processed successfully
Q24. Write a Go program that demonstrates how to use `os/exec` to run shell commands.
Input:
Execute "ls" command using `os/exec`
Expected Output:
List of files: file1, file2, file3
Q25. Write a Go program that implements the producer-consumer pattern using channels.
Input:
Producer sends data, consumer receives and processes data
Expected Output:
Data processed: 5, Data processed: 10
Q26. Write a Go program that demonstrates the use of struct tags to define custom JSON serialization.
Input:
Serialize a struct to JSON with custom tags
Expected Output:
{"name":"John", "age":30}
Q27. Write a Go program that demonstrates how to implement concurrency using `sync.Pool`.
Input:
Use `sync.Pool` to manage memory for frequently used objects
Expected Output:
Object reused from pool
Q28. Write a Go program that demonstrates how to implement a custom HTTP handler.
Input:
Handle requests to "/greet" by returning a greeting message
Expected Output:
Greeting: Hello, World!
Q29. Write a Go program that uses `time.Timer` to schedule a task to run after a specific delay.
Input:
Execute a task after 3 seconds using `time.Timer`
Expected Output:
Task executed after 3 seconds
Q30. Write a Go program that demonstrates how to use `sync.Map` for concurrent-safe maps.
Input:
Add and retrieve values from `sync.Map`
Expected Output:
Retrieved value: Hello
Q31. Write a Go program that demonstrates how to implement a linked list data structure.
Input:
Add values to a linked list and display them
Expected Output:
1 -> 2 -> 3 -> 4
Q32. Write a Go program that uses reflection to compare two struct instances.
Input:
Compare two struct instances with `reflect.DeepEqual`
Expected Output:
Structs are equal
Q33. Write a Go program that demonstrates how to implement a simple cache using `map` and `sync.Mutex`.
Input:
Cache a result of a calculation in a map
Expected Output:
Cached result: 20
Q34. Write a Go program that uses `net/http` to send a POST request with a JSON body.
Input:
Send a POST request with JSON {"name": "Alice"}
Expected Output:
Response: 200 OK
Q35. Write a Go program that demonstrates how to handle file uploads using `http.Request`.
Input:
Upload a file "image.jpg" via HTTP POST
Expected Output:
File uploaded successfully
Q36. Write a Go program that demonstrates how to use `time.Ticker` to execute a task at regular intervals.
Input:
Execute a task every 2 seconds using `time.Ticker`
Expected Output:
Task executed at: 2025-01-14 12:00:02
Q37. Write a Go program that demonstrates how to use `context.WithTimeout` to set a timeout for a task.
Input:
Set a timeout of 2 seconds for an API call
Expected Output:
API call timed out
Q38. Write a Go program that demonstrates how to implement a custom error handling strategy.
Input:
Return a custom error when a specific condition is met
Expected Output:
Error: Invalid operation
Q39. Write a Go program that implements a simple queue using channels.
Input:
Add and remove items from a queue
Expected Output:
Item removed: Task 1
Q40. Write a Go program that demonstrates how to use `unsafe` to perform pointer arithmetic.
Input:
Perform pointer arithmetic with `unsafe`
Expected Output:
Pointer value: 0x7f59df0300b8
Q41. Write a Go program that demonstrates how to use `http.ServeMux` to define custom routes.
Input:
Define routes for "/home" and "/about"
Expected Output:
Response: Welcome to Home
Q42. Write a Go program that demonstrates how to use `strings.Builder` to efficiently build strings.
Input:
Concatenate strings using `strings.Builder`
Expected Output:
Concatenated String: Hello, World!
Q43. Write a Go program that demonstrates how to handle database transactions using `sql.Tx`.
Input:
Start a transaction and insert a record
Expected Output:
Transaction committed
Q44. Write a Go program that demonstrates how to use `http.Cookie` to manage cookies.
Input:
Set a cookie with the name "user" and value "Alice"
Expected Output:
Cookie set: user=Alice
Q45. Write a Go program that demonstrates how to create and use custom Go modules.
Input:
Create a Go module for a math library
Expected Output:
Module initialized
Q46. Write a Go program that demonstrates how to use `os/exec` to run shell commands asynchronously.
Input:
Run a background shell command `sleep 5`
Expected Output:
Command completed after 5 seconds
Q47. Write a Go program that demonstrates how to use `time.Parse` to convert a string to a time.
Input:
Parse the string "2025-01-14 12:00:00"
Expected Output:
Parsed Time: 2025-01-14 12:00:00
Q48. Write a Go program that demonstrates how to use `sync/atomic` to update a counter safely.
Input:
Increment a counter atomically
Expected Output:
Counter: 10
Q49. Write a Go program that demonstrates how to use Go's built-in `sort` package to sort a slice.
Input:
Sort the slice [5, 1, 4, 2, 3]
Expected Output:
Sorted: [1, 2, 3, 4, 5]
Q50. Write a Go program that demonstrates how to use the `http.Request` object to parse query parameters.
Input:
Parse query string "?name=John&age=30"
Expected Output:
Name: John, Age: 30