Two Phase Initialization

I recently ran into two phase initialization at work but didn't remember the name. Basically, two phase initialization looks like this:

type Object struct {
	Res Resource
}

func NewObject() *Object {
	return &Object{}
}

func (o *Object) Init() {
  o.Res = InitializeResource()
}

Why is this bad? Because anyone who wants an Object needs to remember to call two functions, instead of just the one. This can be especially bad in a concurrent environment if another goroutine trys to access the objected created by NewObject before Init is run.

Options to fix this include:

  • create Res inside of NewObject
  • make res Res a parameter of NewObject

In either case, the object is completely constructed by the end of NewObject and you can just use it. I haven't found a good reason to use two phase initialization in Go.

Other resources: