From a 3rd party:
package lib
type Bar interface{
  Age() int
}
Foo(b Bar) int
This doesn't compile because Age is both a method name and field name:
package main
import "lib"
type Person struct {
  Age int
}
func (p *Person)Age() int {
  return p.Age
}
func main() {
  p := Person()
  lib.Foo(p)
}
Without renaming Person.Age, is there a way to call lib.Foo() on an instance of Person?  
Well, not directly, of course, for the reasons already stated. But you could create a wrapper around Person, and pass that in:
type BarPerson struct {
    *Person
}
func (bp *BarPerson) Age() int {
    return bp.Person.Age
}
func main() {
    p := Person{37}
    lib.Foo(&BarPerson{&p})
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With