我该如何在Golang中将结构体作为参数传递给函数?以下是我的代码:
package main
import (
"fmt"
)
type MyClass struct {
Name string
}
func test(class interface{}) {
fmt.Println(class.Name)
}
func main() {
test(MyClass{Name: "Jhon"})
}
当我运行它时,出现了这样的错误。
# command-line-arguments
/tmp/sandbox290239038/main.go:12: class.Name undefined (type interface {} has no field or method Name)
这里有play.golang.org的fiddle地址。