What is the init function in Go?

Answer

The init() function is a special function that runs automatically before main() — it is called by the Go runtime after all variable initializations in the package are complete. A package can have multiple init() functions across multiple files; they all run in the order the files are presented to the compiler. Common uses include registering drivers (sql.Register), validating configuration, setting up global state, and initializing caches. You cannot call init() explicitly — it is only invoked by the runtime.