douyong5825 2016-10-17 12:45
浏览 59
已采纳

我可以在Golang中创建类型的别名吗?

I'm struggling with my learning of Go.

I found this neat implementation of a Set in go: gopkg.in/fatih/set.v0, but I'd prefer naming my sets with a more explicit name that set.Set, doing something like:

type View set.Set

In essence, I want my View type to inherit set.Set's methods. Because, well, View is a set.Set of descriptors. But I know Go is pretty peaky on inheritance, and typing in general.

For now I've been trying the following kinda inheritance, but it's causing loads of errors when trying to use some functions like func Union(set1, set2 Interface, sets ...Interface) Interface or func (s *Set) Merge(t Interface):

type View struct {
    set.Set
}

I'd like to know if there's a way to achieve what I want in a Go-like way, or if I'm just trying to apply my good-ol' OO practices to a language that discards them, please.

  • 写回答

2条回答 默认 最新

  • dongma7725 2017-10-14 00:12
    关注

    If anyone else is coming back to this question, as of Go 1.9 type aliases are now supported.

    A type alias has the form: type T1 = T2

    So in your example you can just do type View = set.Set and everything will work as you want.

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

报告相同问题?