我是 golang 新手,我想实现一个类似于 C++ 重载的重载方法,我的代码如下所示:
type someStruct struct {
val int
some string
}
type object interface {
toByte()
}
// someStruct implementing an object interface
func (s *someStruct) toByte() {
}
func overload(overLoadedObj interface{}) {
switch str := overLoadedObj .(type) {
case string:
fmt.Println("string : ", str)
case int:
fmt.Println("int : ", str)
case object: //* It doesn't come here at all*
fmt.Println("interface obj", str)
}
}
func main() {
overload("hello")
overload(5)
overload(someStruct{val: 5, some: "say"})
}
所以问题是:
如何确保实现对象接口的人将属于案例对象类型?
提前致谢。