Bird
Raised Fist0
Goprogramming~20 mins

Practical use cases in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go code using goroutines?

Consider the following Go program that launches goroutines to print numbers. What will it print?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	for i := 0; i < 3; i++ {
		go func(i int) {
			fmt.Print(i, " ")
		}(i)
	}
	time.Sleep(100 * time.Millisecond)
}
A3 3 3
BCompilation error
C0 0 0
D0 1 2
Attempts:
2 left
💡 Hint

Think about when the goroutines capture the variable i.

❓ Predict Output
intermediate
2:00remaining
What does this Go code print when using channels?

Examine this Go program that uses channels to communicate between goroutines. What is printed?

Go
package main

import "fmt"

func main() {
	ch := make(chan int, 2)
	ch <- 1
	ch <- 2
	fmt.Println(<-ch)
	fmt.Println(<-ch)
}
A2\n1
B1\n2
CCompilation error: channel buffer size mismatch
DDeadlock error at runtime
Attempts:
2 left
💡 Hint

Think about how buffered channels work and the order of sends and receives.

🔧 Debug
advanced
2:00remaining
Why does this Go code cause a deadlock?

Look at this Go program. Why does it cause a deadlock at runtime?

Go
package main

func main() {
	ch := make(chan int)
	ch <- 5
	<-ch
}
ABecause the channel is unbuffered and no goroutine is receiving when sending
BBecause the channel buffer is full
CBecause the receive happens before the send
DBecause the channel is closed before sending
Attempts:
2 left
💡 Hint

Think about how unbuffered channels block on send and receive.

❓ Predict Output
advanced
2:00remaining
What is the output of this Go code using select with multiple channels?

Consider this Go program using select to receive from two channels. What will it print?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan string)
	ch2 := make(chan string)

	go func() {
		time.Sleep(50 * time.Millisecond)
		ch1 <- "first"
	}()

	go func() {
		time.Sleep(10 * time.Millisecond)
		ch2 <- "second"
	}()

	select {
	case msg1 := <-ch1:
		fmt.Println(msg1)
	case msg2 := <-ch2:
		fmt.Println(msg2)
	}
}
ACompilation error: select cases invalid
Bfirst
Csecond
DDeadlock error at runtime
Attempts:
2 left
💡 Hint

Which channel sends first? select picks a ready channel.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using Go's context package in concurrent programs?

Why do Go programmers use the context package when working with concurrent operations?

ATo manage cancellation, deadlines, and passing request-scoped values across goroutines
BTo improve the speed of goroutines by parallelizing CPU instructions
CTo automatically recover from panics in goroutines without crashing the program
DTo serialize data structures for network communication
Attempts:
2 left
💡 Hint

Think about how to stop or control multiple goroutines working on the same task.

Practice

(1/5)
1. Which Go feature is commonly used to create a simple web server that listens on a port?
easy
A. The net/http package with http.ListenAndServe
B. The fmt package with Println
C. The os package with Open
D. The math package with Pow

Solution

  1. Step 1: Identify the package for web servers

    The net/http package provides tools to create web servers in Go.
  2. Step 2: Recognize the function to start the server

    http.ListenAndServe starts a server listening on a specified port.
  3. Final Answer:

    The net/http package with http.ListenAndServe -> Option A
  4. Quick Check:

    Web server = net/http.ListenAndServe [OK]
Hint: Web servers use net/http.ListenAndServe to listen on ports [OK]
Common Mistakes:
  • Confusing printing functions with server functions
  • Using file operations instead of network functions
  • Thinking math package handles networking
2. Which of the following is the correct syntax to open a file named "data.txt" for reading in Go?
easy
A. file, err := os.Open("data.txt")
B. file := os.OpenFile("data.txt", os.O_WRONLY)
C. file, err := ioutil.ReadFile("data.txt")
D. file := os.Read("data.txt")

Solution

  1. Step 1: Identify the function to open a file for reading

    os.Open opens a file in read-only mode and returns a file and error.
  2. Step 2: Check the syntax correctness

    The syntax file, err := os.Open("data.txt") correctly assigns both file and error.
  3. Final Answer:

    file, err := os.Open("data.txt") -> Option A
  4. Quick Check:

    Open file for reading = os.Open [OK]
Hint: Use os.Open with two variables to open files safely [OK]
Common Mistakes:
  • Using os.Read which does not exist
  • Using ioutil.ReadFile which reads whole file, not opens
  • Opening file with wrong flags for reading
3. What will be the output of this Go code snippet?
package main
import "fmt"
func main() {
    nums := []int{2, 4, 6}
    squares := make(map[int]int)
    for _, n := range nums {
        squares[n] = n * n
    }
    fmt.Println(squares)
}
medium
A. map[2:4 4:8 6:12]
B. map[2:4 4:16 6:36]
C. map[2:8 4:16 6:36]
D. map[2:2 4:4 6:6]

Solution

  1. Step 1: Understand the loop and map assignment

    The loop goes through each number in nums and assigns squares[n] = n * n.
  2. Step 2: Calculate each square value

    2*2=4, 4*4=16, 6*6=36, so the map stores these pairs.
  3. Final Answer:

    map[2:4 4:16 6:36] -> Option B
  4. Quick Check:

    Squares map = map with keys and squared values [OK]
Hint: Map keys with squares store n:n*n pairs [OK]
Common Mistakes:
  • Confusing keys and values in map
  • Multiplying incorrectly
  • Expecting slice output instead of map
4. This Go code tries to read a file but has an error. What is the problem?
package main
import (
    "fmt"
    "os"
)
func main() {
    file := os.Open("file.txt")
    defer file.Close()
    fmt.Println("File opened")
}
medium
A. File name should be absolute path
B. Using defer incorrectly
C. Missing error check after os.Open
D. fmt.Println cannot print strings

Solution

  1. Step 1: Check os.Open usage

    os.Open returns two values: file and error. The code only assigns one variable.
  2. Step 2: Identify missing error handling

    Without checking error, the program may panic if file does not open.
  3. Final Answer:

    Missing error check after os.Open -> Option C
  4. Quick Check:

    Always check error from os.Open [OK]
Hint: Always assign and check error from os.Open [OK]
Common Mistakes:
  • Ignoring error returned by os.Open
  • Assuming defer is wrong here
  • Thinking file path must be absolute always
5. You want to create a Go program that reads a list of numbers from a file, squares each number, and writes the results to a new file. Which approach best combines Go features for this task?
hard
A. Use fmt.Println to read and write files directly
B. Use net/http to read files and fmt.Scan to write
C. Use math.Pow to read files and os.Remove to write
D. Use os.Open to read, loop to square, and os.Create with WriteString to save

Solution

  1. Step 1: Reading numbers from a file

    os.Open opens the file for reading; then you can scan or read lines.
  2. Step 2: Processing and writing results

    Loop through numbers, square them, then use os.Create to create output file and WriteString or similar to write results.
  3. Final Answer:

    Use os.Open to read, loop to square, and os.Create with WriteString to save -> Option D
  4. Quick Check:

    File read + process + write = os.Open + loop + os.Create [OK]
Hint: Read with os.Open, write with os.Create, process in loop [OK]
Common Mistakes:
  • Using fmt.Println for file I/O
  • Confusing math.Pow with file reading
  • Using network packages for file tasks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard