So you think you know Go?

Serge Gotsuliak
1 min readMay 17, 2020

--

Golang pretend to be very simple and beginners friendly programming language. If you think so, try to guess the output of four small programs. And maybe you’ll be very surprised with results :)

Here also links to Go playground to check out the actual output.

Quiz #1: https://play.golang.org/p/WGPJeHqtv1j

package mainimport "fmt"func surprise(a []int) {
for i := range(a) {
a[i] = 5
}
fmt.Println(a)
}
// Quiz #1func main() {
a := []int{1, 2, 3, 4}
surprise(a)
fmt.Println(a)
}

Quiz #2: https://play.golang.org/p/w7nAvEMdjIt

package mainimport "fmt"func surprise(a []int) {
a = append(a, 5)
for i := range(a) {
a[i] = 5
}
fmt.Println(a)
}
// Quiz #2func main() {
a := []int{1, 2, 3, 4}
surprise(a)
fmt.Println(a)
}

Quiz #3: https://play.golang.org/p/F5vqKP87PeR

package mainimport "fmt"func surprise(a []int) {
a = append(a, 5)
for i := range(a) {
a[i] = 5
}
fmt.Println(a)
}
// Quiz #3func main() {
a := []int{1, 2, 3, 4}
surprise(a)
a = append(a, 5)
fmt.Println(a)
}

Quiz #4: https://play.golang.org/p/UHClB-dI1YK

package mainimport "fmt"func surprise(a []int) {
a = append(a, 5)
for i := range(a) {
a[i] = 5
}
fmt.Println(a)
}
// Quiz #4func main() {
a := []int{1, 2, 3, 4}
a = append(a, 5)
surprise(a)
fmt.Println(a)
}

Gave the correct answers to all quizes? Maybe confused with some of them? Please share your results :)

--

--