[Go] init()

Jiwoo Kim·2021년 11월 9일
0

Go

목록 보기
10/11

init()

Go 프로그램은 main 패키지의 main() 함수에서 시작함. 단, 패키지에 init()이 정의되어 있으면 main() 전에 먼저 수행하고 패키지 변수들을 초기화함.

초기화 순서

  1. initialization of imported packages (recursive definition)
  2. computing and assigning initial values for variables declared in a package block
  3. executing init functions inside the package

+) Package initialization is done only once even if package is imported many times.

init()은 호출할 수 없다

init function doesn’t take arguments neither returns any value. In contrast to main, identifier init is not declared so cannot be referenced:

호출하면 컴파일 시점에 undefined: init error

usage

a common use of init functions is to verify or repair correctness of the program state before real execution begins.

0개의 댓글