Init function
An init function is a special Go package-level function that runs automatically during package initialization before ordinary program execution.
100 Go Mistakes and How to Avoid Them
An init function is a special Go function that runs automatically to perform package initialization before program execution begins.
- How it is used
- Uses automatic package initialization for state setup and deliberate side effects, while cautioning against hiding application startup inside init.
- How deep it goes
- Explains signatures, package dependencies, file ordering, blank imports, and testing and error-handling trade-offs, but not runtime internals.
- Examples
- Demonstrates variable-init output, dependent redis and main packages, multiple init functions, blank imports, database setup, and HTTP handler registration.
- What you can do after
- Diagnose initialization-order and startup-design problems, then move failure-prone setup into caller-controlled functions.
Read how 100 Go Mistakes and How to Avoid Them explains init function →
Go by Example
An init function is a special Go function that runs automatically during package initialization before program execution begins.
- How it is used
- Registers database drivers automatically through blank imports during package initialization.
- How deep it goes
- Explains one-time implicit execution and demonstrates registry registration, while recommending explicit initialization for most other cases.
- Examples
- Demonstrates blank-importing modernc.org/sqlite and registering the driver in database/sql.
- What you can do after
- Operate SQLite driver registration by adding a blank import and understanding its automatic initialization.
Also covered in
What to know first
- GoGo is a statically typed, compiled, general-purpose programming language with garbage collection and built-in support for concurrency.
Related concepts
- packagepart of · A software package is an organized unit of related software components, defined by a programming language or package ecosystem for grouping, naming, building, importing, or distribution.
- connection poolinguses · Connection pooling is a resource-management technique that maintains a managed collection of established connections for reuse across operations.
- exceptioncontrasts with · An exception is a runtime signal or condition representing an abnormal event that interrupts or alters ordinary program flow and can be handled or propagated.
- command-line argumentconfigures · A command-line argument is a value or option supplied to a program when it is invoked, providing input or affecting its behavior.
- side effectuses · A side effect is an observable change or interaction caused by a computation beyond producing its direct result, such as modifying state or interacting with the external environment.
Questions about init function
What is init function?
An init function is a special Go package-level function that runs automatically during package initialization before ordinary program execution.
Which Manning books explain init function?
100 Go Mistakes and How to Avoid Them and Go by Example.
What should I know before learning init function?
Manning books that teach init function usually introduce Go first.
What is the difference between init function and exception?
An init function is a special Go package-level function that runs automatically during package initialization before ordinary program execution. An exception is a runtime signal or condition representing an abnormal event that interrupts or alters ordinary program flow and can be handled or propagated.
What is init function part of?
Init function is part of package. A software package is an organized unit of related software components, defined by a programming language or package ecosystem for grouping, naming, building, importing, or distribution.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

