Empty interface{} in function type

后端 未结 2 865
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 09:44

An object of any type can be assigned to an empty interface. For example, we have the following function

func Println(i interface{} ) {
  fmt.Println(i)
}
         


        
2条回答
  •  深忆病人
    2021-02-04 10:24

    It fails because the signatures don't match.

    When you call Println(3), the function isn't taking an integer as its first argument. Rather the integer gets packed inside an interface{} variable (an automatic conversion, since integers conform to the interface), and that variable is passed to the function. This conversion happens on the calling side, so the process of calling the function is different to calling a function matching func(i int).

    If you want to write a function that accepts arbitrary unary functions, you will need to declare it to take an interface{} variable as its argument and then check the value using the reflect package. The reflect package can also help you call arbitrary functions where you don't know the signature at compile time.

    For example:

    func Map(f, v interface{}) interface{} {
        fn := reflect.ValueOf(f)
        fnType := fn.Type()
        if fnType.Kind() != reflect.Func || fnType.NumIn() != 1 || fnType.NumOut() != 1 {
            panic("Expected a unary function returning a single value")
        }
        res := fn.Call([]reflect.Value{reflect.ValueOf(v)})
        return res[0].Interface()
    }
    

    This will call the given function f with the argument v and return the result. Provided v is assignable to f's first argument the call will succeed without a panic. You can experiment with this example here: http://play.golang.org/p/kkBu56JYb8

提交回复
热议问题