dongqiyang3711 2013-12-31 02:29 采纳率: 100%
浏览 30
已采纳

在Go中输入变量

In Haskell, map has type:

map :: (a -> b) -> [a] -> [b]

Note that a and b are not absolute types but type variables, which mean that they can be any type as long as each variable always refer to the same type in a specific function call. How to do the same thing in Go?

  • 写回答

2条回答 默认 最新

  • doudun3040 2013-12-31 02:46
    关注

    Go does not have a Hindley-Milner type system like Haskell's, so it can't express exactly the same things, like types with variables. The way type-agnostic functions are done in Go is with interfaces. If you want to express "any type", that's usually written as an empty interface (interface{}). The type of the Map function in the standard library is:

    func Map(iter Iterable, f func(interface{}) interface{}) Iterable
    

    So instead of taking a function that goes from a to b, it takes a function that goes from interface{} to interface{}.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?