title: When is the init() function run?
body: <p>I've tried to find a precise explanation of what the <code>init()</code> function does in Go. I read what <a href="https://golang.org/doc/effective_go.html#init" rel="noreferrer">Effective Go</a> says but I was unsure if I understood fully what it said. The exact sentence I am unsure is the following:</p>
  <blockquote>
  <p>And finally means finally: init is called after all the variable declarations in the package have evaluated their initializers, and those are evaluated only after all the imported packages have been initialized.</p>
  </blockquote>
  <p>What does <code>all the variable declarations in the package have evaluated their initializers</code> mean? Does it mean if you declare &quot;global&quot; variables in a package and its files, init() will not run until all of it is evaluated and then it will run all the init function and then main() when ./main_file_name is ran?</p>
  <p>I also read Mark Summerfield's go book the following:</p>
  <blockquote>
  <p>If a package has one or more init() functions they are automatically executed before the main package's main() function is called.</p>
  </blockquote>
  <p>In my understanding, <code>init()</code> is only relevant when you intend to run main() right? Anyone understands more precisely <code>init()</code> feel free to correct me</p>