How can to initialize any fields in golang types? For example:
type MyType struct {
Field string = "default"
}
How can to initialize any fields in golang types? For example:
type MyType struct {
Field string = "default"
}
You can't have "default" values like that, you can either create a default "constructor" function that will return the defaults or simply assume that an empty / zero value is the "default".
type MyType struct {
Field string
}
func New(fld string) *MyType {
return &MyType{Field: fld}
}
func Default() *MyType {
return &MyType{Field: "default"}
}
Also I highly recommend going through Effective Go.