I am learning Go right now and would like to initialize a type using variadic arguments without using reflection. Is it possible?
Here an example:
type MyType struct {
field1 string
field2 string
...
fieldN string
}
func CreateMyType(arguments ...string) *MyType {
inst := MyType{arguments...} // does not work, is there any other way???
return &inst
}
Note It makes me really sad, that the question is downvoted, where I ask legitimate things and try to learn out of them :(
It's possible with a little bit of code:
func CreateMyType(arguments ...string) *MyType {
var m MyType
switch len(arguments) {
case 3:
m.field3 = arguments[2]
fallthrough
case 2:
m.field2 = arguments[1]
fallthrough
case 1:
m.field1 = arguments[0]
}
return &m
}