Go has built-in support for concurrency. Which feature makes it easy to write programs that do many things at once?
Think about what Go provides to run multiple tasks without heavy system threads.
Go uses goroutines, which are very lightweight and easy to start. This makes concurrent programming simpler and efficient.
What is the output of this Go program?
package main import ( "fmt" "time" ) func main() { ch := make(chan string) go func() { time.Sleep(100 * time.Millisecond) ch <- "Hello" }() msg := <-ch fmt.Println(msg) }
Consider how channels synchronize goroutines.
The goroutine sends "Hello" to the channel after 100ms. The main goroutine waits to receive it, then prints it.
Consider this Go code snippet. What will it print?
package main import "fmt" func main() { m := map[string]int{"a": 1, "b": 2} for k, v := range m { if k == "a" { m["c"] = 3 } fmt.Println(k, v) } }
Think about modifying a map while iterating over it.
In Go, writing to a map while iterating causes a runtime panic due to concurrent map iteration and write.
Go is designed to be simple and easy to read. Which of these is NOT a reason why this simplicity helps developers?
Think about how simplicity affects code clarity and teamwork.
Go's simplicity encourages clear and concise code, not verbose or complicated code.
What will this Go program print?
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup for i := 0; i < 3; i++ { wg.Add(1) go func(i int) { defer wg.Done() fmt.Print(i) }(i) } wg.Wait() }
Think about how goroutines run concurrently and print order.
The goroutines run concurrently and print their index. The order is not guaranteed, so any permutation of 0,1,2 is possible.