75

I have a problem trying to import a type from another package and file. The struct that I'm trying to import is the one underneath.

type PriorityQueue []*Item

type Item struct {
   value string
   priority int   
   index int 
}

If I would put the PriorityQueue alongside with all of its methods in the same file I'd declare it with

pq:= &PriorityQueue{}

I've been searching the internet like a madman for an answer on this simple question but I have not found an answer. I usually program in java and import classes is so elementary.

5
  • 1
    Why wouldn't you just copy the struct to your package? Commented Apr 27, 2015 at 14:32
  • 1
    Or import the package? Commented Apr 27, 2015 at 14:33
  • 5
    Qualify it. pq := &mypkg.PriorityQueue{}. Commented Apr 27, 2015 at 14:33
  • 2
    possible duplicate of how do I use my import package's struct as a type in go Commented Apr 27, 2015 at 14:37
  • 4
    I faced the same problem, The fix is to use caps not just for the struct name but the variables inside struct as well for them to be exported. Commented May 8, 2019 at 10:46

3 Answers 3

104

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have also made the mistake where I create a new project outside of the GOPATH. Local imports failing may be the first sign that this has been done.
It's awesome to use import . for leaving out the package name completely!
23

Also, if you want to export a struct and init this struct value outside of it's package than all fields of the struct must start with capital letter, otherwise you will get an error "Unexported field 'fieldName' usage"

type Item struct {
   Value string  // uppercase V
   Priority int  // uppercase P
   Index int     // uppercase I
}

Thanks to @Vasantha Ganesh comment

2 Comments

Thanks a lot man, this is exactly what I was searching for
This is something that I think al go beginner, especially those who come from JavaScript world, need to remember that anything to be used outside the package need to be Capitalized.
16

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

...

2 Comments

How do I access 'X' from myTypes? Because, in a similar scenario, myTypes.A.X doesnt seem to work.
To access the variable inside struct simply do myTypes.X and make sure X is global variable(first letter should be in capital)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.