v
v copied to clipboard
Not initialized member variable throw interface
struct Dog {
a int
}
fn (d Dog) speak() int {
println('$d.a')
return 12345 // dummy
}
interface Speaker {
speak() int
}
fn perform(s Speaker) {
s.speak()
}
fn main() {
dog := Dog{}
println('$dog.a') // -> 0
perform(dog)
println('$dog.a') // -> 0
}
Actually
0
0
0
In fact
0
-375720792 // random
0
on 0.0.7
Thanks. A minor note: you can do println(d.a) instead of println('$d.a').
i can't understand what interface use for. i can't pass var to it, so, this interface speak() just call struct's method without any var. i not study cpp, i think it like cpp's polymorphism. but,interface don't pass variable, how i can use it? need someone give me some examples to study.