我可以在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.

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.

Note, I think the simple aliasing you proposed initially is syntactically valid though having had a quick look at the set library, rather than aliasing set.Set it might make more sense to alias set.Interface, e.g.:

package main

import (
  "fmt"

  set "gopkg.in/fatih/set.v0"
)

// View is a type alias for the set.Interface interface
type View set.Interface

// Display takes one of our View types in order to print it.
func Display(view View) {
  fmt.Println(view.List())
}

func main() {
  // create our first set.Interface or View
  v1 := set.New()
  v1.Add("foo")

  // create our second set.Interface or View
  v2 := set.New("bar")

  // call a set function
  v3 := set.Union(v1, v2)

  // call our function that takes a View
  Display(v3)
}

You may have noticed I'm cheating somehow because I make no real mention of the aliased type in the above code other than in defining the parameter to the Display function above which you'll note takes in a View instance rather than a set.Interface. If you have lots of functions working on these things, then that might read more expressively for your domain.

Note that because our View type is an alias to an interface type, it precludes adding your own functions to that type as Go doesn't allow us to have an interface receiver type for a function (I might be expressing that incorrectly). By this I mean that you can't do anything like:

func (v View) Display() string {
  return v.String()
}

In summary I think aliasing things is fine, it can make internal APIs more readable, and you can lean on the compiler to help eliminate certain classes of errors; however this doesn't allow you to add functionality to the custom type. If this is required an alternate approach would be necessary, either embedding or simple composition (i.e. a View has a Set).