I had written a lazy code to demonstrate the issue I am having to implement interfaces. I have methods M1, M2 that take struct X as an argument and have a type of struct Y. I wanted all these methods to be implemented by a single interface I. The issue is the method M that implements the interface I needs to be aware of the arguments that are required to be passed to the child methods (M1,M2). I get an error: <argument name> used as a value
when I pass multiple arguments in M
type Y struct {
a int
}
type X struct {
a int
}
(y *Y) func M1(x X) struct {
return y.a+x.a
}
(y *Y) func M2(x X) struct {
return y.a*x.a
}
type I interface {
M1(x X)
M2(x X)
}
func M(i I, x X) {
fmt.println(i.M1(x)) //returns an error i.M1(x) used as a value
fmt.println(i.M2(x)) //returns an error i.M2(x) used as a value
}
The problem that cause <argument name> used as a value
error in your example is that functions that form interface I
are declared to have no return value:
type I interface {
M1(x X)
M2(x X)
}
Surely you can't pass function-call as argument of Println
if the function returns nothing: fmt.println(i.M1(x))
. Change the interface declaration in your example to return something (and a few more fixes*):
type Y struct {
a int
}
type X struct {
a int
}
func(y *Y) M1(x X) int {
return y.a+x.a
}
func(y *Y) M2(x X) int {
return y.a*x.a
}
type I interface {
M1(x X) int
M2(x X) int
}
func M(i I, x X) {
fmt.Println(i.M1(x))
fmt.Println(i.M2(x))
}
*) change M1
& M2
to return int
instead of struct
and fix syntax for function declaration with receiver