Recall & Review
beginner
What is the purpose of the
init function in Go?The
init function is used to initialize variables or state before the main program runs. It runs automatically when the package is loaded, without being called explicitly.Click to reveal answer
intermediate
How many
init functions can a single Go package have?A Go package can have multiple
init functions, even within the same file or across different files in the package. All will run in the order the compiler decides.Click to reveal answer
beginner
When exactly are
init functions executed in a Go program?They run automatically after all package-level variables are initialized and before the
main function starts.Click to reveal answer
beginner
Can you call an
init function explicitly in Go code?No,
init functions cannot be called explicitly. They are called automatically by the Go runtime during program startup.Click to reveal answer
intermediate
What happens if multiple packages have
init functions?All
init functions in imported packages run first, in the order of imports, before the init functions in the main package run.Click to reveal answer
What is the correct way to define an
init function in Go?✗ Incorrect
The
init function must be named exactly init with no parameters or return values.When does the
init function run in a Go program?✗ Incorrect
The
init function runs automatically after package variables are set and before main.Can you have more than one
init function in a single Go package?✗ Incorrect
Multiple
init functions can exist in the same package and will all run.What happens if you try to call
init() explicitly in your code?✗ Incorrect
init functions cannot be called explicitly; attempting to do so causes a compile error.If package A imports package B, when does package B's
init run?✗ Incorrect
Imported packages'
init functions run before the importing package's init.Explain the role and behavior of the
init function in Go module initialization.Think about what happens before your program starts running main.
You got /4 concepts.
Describe how Go handles multiple
init functions across different packages during program startup.Consider the sequence of package loading.
You got /4 concepts.