0
0
Goprogramming~20 mins

Module initialization in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Module Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of init function execution order

Consider the following Go program with multiple init functions across two files in the same package. What will be the output when this program runs?

Go
package main

import "fmt"

func init() {
    fmt.Println("Init function 1")
}

func init() {
    fmt.Println("Init function 2")
}

func main() {
    fmt.Println("Main function")
}
A
Main function
Init function 1
Init function 2
B
Init function 2
Init function 1
Main function
C
Init function 1
Init function 2
Main function
D
Init function 1
Main function
Init function 2
Attempts:
2 left
💡 Hint

Remember that all init functions in a package run before main, and their order is the order they appear in the file.

Predict Output
intermediate
2:00remaining
Output of init in multiple packages

Given two packages pkg1 and main, each with an init function, what is the output when the program runs?

Go
package main

import (
    "fmt"
    "example.com/pkg1"
)

func init() {
    fmt.Println("Init in main")
}

func main() {
    fmt.Println("Main function")
}

// pkg1 package code:
// package pkg1
// import "fmt"
// func init() {
//     fmt.Println("Init in pkg1")
// }
A
Init in main
Init in pkg1
Main function
B
Main function
Init in pkg1
Init in main
C
Init in pkg1
Main function
Init in main
D
Init in pkg1
Init in main
Main function
Attempts:
2 left
💡 Hint

Think about package import order and when init functions run.

🔧 Debug
advanced
2:00remaining
Why does this init function cause a runtime panic?

Examine the following Go code. Why does it cause a runtime panic when the program starts?

Go
package main

import "fmt"

var x = initialize()

func initialize() int {
    fmt.Println("Initializing x")
    panic("panic during initialization")
    return 42
}

func main() {
    fmt.Println("Main function")
}
AThe panic occurs because <code>initialize</code> is called during variable initialization before <code>main</code> runs.
BThe panic occurs because <code>init</code> function is missing and Go requires it.
CThe panic occurs because <code>main</code> calls <code>initialize</code> explicitly and panics.
DThe panic occurs because <code>fmt.Println</code> cannot be called outside <code>main</code>.
Attempts:
2 left
💡 Hint

Think about when package-level variables are initialized.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in init function

Which option contains a syntax error in the init function declaration?

A
func init() string {
    return "Init"
}
B
func init() {
    // empty init
}
C
func init() {
    fmt.Println("Starting")
}
D
func init() {
    fmt.Println("Init called")
}
Attempts:
2 left
💡 Hint

Remember the init function signature rules in Go.

🚀 Application
expert
2:00remaining
How many times does init run in this program?

Given the following Go program with multiple packages and files, how many times will init functions run in total when the program starts?

Assume:

  • Package main has 2 files, each with 1 init function.
  • Package utils has 1 file with 2 init functions.
  • Package config has 1 file with 1 init function.
  • main imports utils and config.
A4
B5
C6
D7
Attempts:
2 left
💡 Hint

Count all init functions in all imported packages and main.