Import struct from another package and file golang

What @icza said above plus:

With Go 1.9 there are type aliases that allow you to import types from packages and alias them into what look like local types:

package.go contents:

type A struct {
    X, Y int
}

main.go contents:

...

import myTypes "path/to/package"

// Note the equal sign (not empty space)
// It does NOT create a new "subclass"
// It's an actual alias that is local.
// Allows you to avoid whole-sale `import . "path/to/package"` which imports all objects from there into local scope.
type A = myTypes.A

...

In Go you don't import types or functions, you import packages (see Spec: Import declarations).

An example import declaration:

import "container/list"

And by importing a package you get access to all of its exported identifiers and you can refer to them as packagename.Identifiername, for example:

var mylist *list.List = list.New()

// Or simply:
l := list.New()

There are some tricks in import declaration, for example by doing:

import m "container/list"

You could refer to the exported identifiers with "m.Identifiername", e.g.

l := m.New()

Also by doing:

import . "container/list"

You can leave out the package name completely:

l := New()

But only use these "in emergency" or when there are name collisions (which are rare).

Tags:

Struct

Package

Go