oop - Constructors in Go -
i have struct , initialised sensible default values.
typically, thing here use constructor since go isn't oop in traditional sense these aren't true objects , has no constructors.
i have noticed init method @ package level. there else similar can used @ struct level?
if not accepted best practice type of thing in go?
there 2 accepted best practices:
- make 0 value of struct sensible default. (while looks strange people coming "traditional" oop works , convenient).
- provide function
func new() yourtypor if have several such types in package functionsfunc newyourtype1() yourtype1, on.
document if 0 value of type usable or not (in case has set 1 of new... functions. (for "traditionalist" oops: not read documentation won't able use types properly, if cannot create objects in undefined states.)
Comments
Post a Comment