I am struggling to understand how I can write this appropriately, what is different about func
s that I cannot assign them to a field within a struct
via the struct
s pointer?
I receive a s.Bar undefined (type any has no field or method Bar)
however I can access f.Text
after assignment just fine.
https://go.dev/play/p/JuQp7zcozBm
type test func()
func hi() {
fmt.Println("i work")
}
type Foo struct {
Text string
Bar test
}
func BigTest(s any) {
f := s.(*Foo)
f.Bar = hi
f.Text = "something"
fmt.Println(f.Text)
s.Bar()
}
func main() {
f := Foo{}
BigTest(&f)
}
CodePudding user response:
You have to call f.Bar()
not s.Bar()
because s
is type any
, and f
is the one you type asserted.