Scope and initialization

Package init functions

An init function has no parameters or results and cannot be called by name. It runs after package variables initialize; imported packages initialize before the importing package. Multiple init functions are permitted. Example output: 'ready'.

Formal Syntax & Structure

go syntaxgo
func init() { statements }

Working Example

Example Snippetgo
package main
import "fmt"
var state = "starting"
func init() { state = "ready" }func main() {  fmt.Println(state)}
Go Reference