...

/

Puzzle 21 Explanation: init Is Special

Puzzle 21 Explanation: init Is Special

Understand how init works in Go.

We'll cover the following...

Try it yourself

Try executing the code below to see the result for yourself.

Press + to interact
package main
import (
"fmt"
)
func init() {
fmt.Printf("A ")
}
func init() {
fmt.Print("B ")
}
func main() {
fmt.Println()
}

Explanation

In Go, init presents a particular case. We cannot define two functions with the same name in the same package in Go compiler. However, init is different.

Go’s documentation describes it like this: ...

Access this course and 1400+ top-rated courses and projects.