0
0
Goprogramming~5 mins

Module initialization in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afunc init() { /* code */ }
Bfunc initialize() { /* code */ }
Cfunc Init() { /* code */ }
Dfunc start() { /* code */ }
When does the init function run in a Go program?
ABefore package-level variables are initialized
BAfter the <code>main</code> function runs
COnly when called explicitly
DAfter package-level variables are initialized and before <code>main</code>
Can you have more than one init function in a single Go package?
ANo, only one is allowed
BYes, multiple are allowed
COnly one per file
DOnly if they have different names
What happens if you try to call init() explicitly in your code?
AIt is ignored
BIt runs normally
CIt causes a compile error
DIt runs but only once
If package A imports package B, when does package B's init run?
ABefore package A's <code>init</code>
BAfter package A's <code>init</code>
CAt the same time as package A's <code>init</code>
DOnly if package B is used in <code>main</code>
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.