type Test struct {
a int
b int
}
func f(a, b int) (int, int) {
return a, b
}
k := Test{(f(5, 6))} // Error
but
type Test struct {
a int
}
func f(a int) int {
return a
}
k := Test{(f(5))} // Ok
How send func result to struct fields, or create method for group of types,
example:
func(MyType1, MyType2) f (something) something {}
New contributor
Сергей Ковальчук is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Because
k := Test{ f(5, 6) }
is equivalent to passing an array of size two, not two arguments. Think f([1, 2])
vs f(1, 2)
.
There’s a spread/spread function for structures inside structures, but unfortunately none for arrays inside structures.
So there’s no easy solution, you’ll have to assign them yourself.