gotypesalias

Can I create an alias of a type in 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.


Solution

  • 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.