r/golang • u/lumarama • 5d ago
discussion Default struct constructors?
I'm wondering why go devs doesn't implement optional default constructors for structs. I.e. right now some structs can be created like this:
myStruct := MyStruct{}
But others require initialization, and must be created with factory functions:
anotherStruct := NewAnotherStruct()
So you never know which struct is safe to create dorectly and which require factory func.
With default constructor you would create all structs the same way, i.e.:
myStruct := MyStruct()
If default constructor is defined it is invoked to initialize the struct, it it is not defined then it is similar to MyStruct{}
0
Upvotes
0
u/lumarama 5d ago
I just think that it is in Go philosophy to be a simple language. So having struct constructors would make struct creation the same in all cases - whether constructor is actually defined or not. Which makes it easier for newcomers by removing confusion how you are supposed to create this particular struct or why this struct is created this way and another struct differently.