Generic Functions in Go

筅森魡賤 提交于 2020-01-21 16:17:52

问题


I'm in the process of learning Go and the documentation and interactive lessons say that an empty interface can hold any type, as it requires no additionally implemented methods.

So for an example:

func describe(i interace{}) {
    fmt.Printf("Type: %T | Value: %v\n", i, i)
}

...would print out...

"Type: int | Value: 5" // for i := 5
"Type: string | Value: test" // for i := "test"
... etc

So I guess my question is if this is Go's way of implementing generic functions or if there is another, more suitable, way of doing them.


回答1:


The Go paradigm is generally to avoid this by implementing the behavior in non-empty interfaces. For example, say you wanted to print something with type-specific formatting:

func Print(i interface{}) {
    switch o := i.(type) {
        case int64:
            fmt.Printf("%5d\n", o)
        case float64:
            fmt.Printf("%7.3f\n", o)
        case string:
            fmt.Printf("%s\n", o)
        default: // covers structs and such
            fmt.Printf("%+v\n", o)
    }
}

Alternatively, you could define an interface for things that know how to string themselves (this exists in the base library as an fmt.Stringer), and use that:

type Stringer interface {
    String() string
}

func Print(o Stringer) {
    fmt.Println(o.String())
}

type Foo struct {
    a, b int
}

func (f Foo) String() string {
    // Let's use a custom output format that differs from %+v
    return fmt.Sprintf("%d(%d)", f.a, f.b) 
}

type Bar struct {
    t bool
}

func (b Bar) String() string {
    if b.t {
        return "TRUE! =D"
    }
    return "false =("
}

https://play.golang.org/p/Ez6Hez6cAv

This lets you have a generic-like functionality, but still retain type safety and have the behavior itself defined by the types, rather than your generic function.

Go encourages you to think of types in this way, based on their behavior, what they can do rather than what they contain.




回答2:


Golang doesn't have a generic type, so the way you get around this is passing interface type and using type switches within the function.



来源:https://stackoverflow.com/questions/42726986/generic-functions-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!