# README
Use constructor functions
Create a function that initializes a Company with a properly initialized Department implementation.
This solution fixes the nil interface problem in several ways:
-
Guaranteed Initialization: The constructor function
NewCompany
requires aDepartment
as an argument, ensuring that aCompany
is always created with a non-nilPrimaryDepartment
. -
Encapsulation: The creation logic is encapsulated in the constructor, reducing the chance of incorrect initialization elsewhere in the code.
-
Clear Intent: Using a constructor function makes it clear to other developers that proper initialization is required and how it should be done.
-
Compile-time Safety: If a developer tries to create a
Company
without providing aDepartment
, they'll get a compile-time error rather than a runtime panic. -
Flexibility: The constructor can be easily extended to include additional initialization logic or validation if needed in the future.
By using this pattern, we shift the responsibility of providing a valid Department
to the caller of NewCompany
, making the code more robust and less prone to runtime errors. This approach aligns with Go's philosophy of making the zero value useful, as a Company
created with the constructor is immediately usable without risk of nil pointer dereferences.
package core
import (
"fmt"
"github.com/go-logr/logr"
)
type Department interface {
GetName() string
}
type Company struct {
PrimaryDepartment Department
}
func (c *Company) DisplayInfo() {
fmt.Printf("Company's primary department: %s\n", c.PrimaryDepartment.GetName())
}
func NewCompany(dept Department) *Company {
return &Company{
PrimaryDepartment: dept,
}
}
type StandardDepartment struct {
name string
}
func (d *StandardDepartment) GetName() string {
return d.name
}
func NewStandardDepartment(name string) *StandardDepartment {
return &StandardDepartment{name: name}
}
func Main(logger logr.Logger) {
logger.V(1).Info("Debug: Entering Main function")
dept := NewStandardDepartment("Engineering")
myCompany := NewCompany(dept)
myCompany.DisplayInfo()
}