Bird
Raised Fist0
Goprogramming~20 mins

Module initialization in Go - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the init function in a Go module?
easy
A. To run setup code automatically before main starts
B. To define the main entry point of the program
C. To declare global variables
D. To handle user input during runtime

Solution

  1. Step 1: Understand the role of init in Go

    The init function is designed to run automatically before the main function starts.
  2. Step 2: Differentiate from other functions

    Unlike main, init is used for setup tasks, not for program entry or input handling.
  3. Final Answer:

    To run setup code automatically before main starts -> Option A
  4. Quick Check:

    init runs before main [OK]
Hint: Remember: init runs before main automatically [OK]
Common Mistakes:
  • Confusing init with main
  • Thinking init handles user input
  • Assuming init must be called manually
2. Which of the following is the correct syntax to declare an init function in Go?
easy
A. init func() {}
B. func init() {}
C. function init() {}
D. func Init() {}

Solution

  1. Step 1: Recall Go function syntax

    In Go, functions start with the keyword func, followed by the function name and parentheses.
  2. Step 2: Check init function naming rules

    The init function must be lowercase and have no parameters or return values.
  3. Final Answer:

    func init() {} -> Option B
  4. Quick Check:

    Correct init syntax = func init() {} [OK]
Hint: Use lowercase init with empty parentheses [OK]
Common Mistakes:
  • Capitalizing Init instead of init
  • Using wrong keywords like function
  • Adding parameters or return types to init
3. What will be the output of this Go program?
package main
import "fmt"
func init() {
    fmt.Println("Init 1")
}
func init() {
    fmt.Println("Init 2")
}
func main() {
    fmt.Println("Main")
}
medium
A. Init 2 Init 1 Main
B. Main Init 1 Init 2
C. Init 1 Init 2 Main
D. Init 1 Main Init 2

Solution

  1. Step 1: Understand multiple init functions execution order

    All init functions run automatically before main, in the order they appear in the source file.
  2. Step 2: Trace the output sequence

    First, "Init 1" prints, then "Init 2", and finally "Main" from the main function.
  3. Final Answer:

    Init 1 Init 2 Main -> Option C
  4. Quick Check:

    Multiple init run in order before main [OK]
Hint: Multiple init run top to bottom before main [OK]
Common Mistakes:
  • Assuming main runs before init
  • Thinking init functions run in random order
  • Confusing output order of init functions
4. Identify the error in this Go code snippet related to init function:
package main
func init() {
    fmt.Println("Starting")
}
func main() {
    fmt.Println("Running")
}
medium
A. main function must be named Main
B. init function cannot print output
C. Multiple init functions are required
D. Missing import of the fmt package

Solution

  1. Step 1: Check for required imports

    The code uses fmt.Println but does not import the fmt package, causing a compile error.
  2. Step 2: Validate other code parts

    init can print output, multiple init are optional, and main must be lowercase.
  3. Final Answer:

    Missing import of the fmt package -> Option D
  4. Quick Check:

    Use import "fmt" to fix [OK]
Hint: Always import packages before using their functions [OK]
Common Mistakes:
  • Forgetting to import fmt
  • Thinking init cannot print
  • Misnaming main function
5. You want to initialize a configuration map before your Go program runs. Which approach correctly uses init to do this?
var config map[string]string

func init() {
    // What should go here?
}
hard
A. config = make(map[string]string) config["env"] = "production"
B. config := map[string]string{"env": "production"}
C. var config = map[string]string{"env": "production"}
D. config = nil

Solution

  1. Step 1: Understand map initialization in init

    The global config variable must be assigned a new map using make before adding keys.
  2. Step 2: Check each option

    config = make(map[string]string) config["env"] = "production" correctly initializes and sets a key. config := map[string]string{"env": "production"} uses := which creates a new local variable, not affecting the global. var config = map[string]string{"env": "production"} declares a local config inside init, not affecting the global. config = nil sets config to nil, which is not useful.
  3. Final Answer:

    config = make(map[string]string) config["env"] = "production" -> Option A
  4. Quick Check:

    Use make to initialize maps before use [OK]
Hint: Use make to initialize maps before adding keys [OK]
Common Mistakes:
  • Using := inside init creates local variable
  • Not initializing map before assignment
  • Setting map to nil instead of a new map
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