Skip to content

Instantly share code, notes, and snippets.

@iansmith
Created October 29, 2011 11:36
Show Gist options
  • Save iansmith/1324357 to your computer and use it in GitHub Desktop.
Save iansmith/1324357 to your computer and use it in GitHub Desktop.
suprising for a naive Java or C++ developer
package main
// see https://gist.github.com/1324737 for a java version of the same issue, which does work
type fruit interface {
foo() int
}
type apple interface {
fruit
bar() int
}
func needFruit(f fruit) {
}
func needManyFruits(f []fruit) {
}
func f() {
var f fruit
var a apple
needFruit(f)
needFruit(a) //duck typing plus the "borrowing" of interface fruit into apple works like inheritance
var fs []fruit
var as []apple
needManyFruits(fs)
needManyFruits(as) //this doesn't work! a naive C++ or java developer is surprised!
//there is a hint of an explanation here: http://golang.org/doc/go_faq.html#convert_slice_of_interface
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment