1

Following this answer I have created the following project structure:

.
├── bin
├── pkg
└── src
    └── github.com
        └── GITHUB_USERNAME
            └── PROJECTNAME
                ├── lib
                │   └── model.go
                │   └── ... .go
                ├── LICENSE
                ├── README.md
                └── PROJECTNAME.go
  • PROJECTNAME.go has the package main
  • model.go has the package PROJECTNAME

In the PROJECTNAME.go I am importing the follwing:

import(
    'github.com/GITHUB_USERNAME/PROJECTNAME/lib/model'
) 

but when I run go build I get the follwing error:

 cannot find package "github.com/GITHUB_USERNAME/PROJECTNAME/lib/model" in any of:
    /usr/lib/go/src/pkg/github.com/GITHUB_USERNAME/PROJECTNAME/lib/model (from $GOROOT)
    /home/USERNAME/go/src/github.com/GITHUB_USERNAME/PROJECTNAME/lib/model (from $GOPATH)

How must the packages names be to import correctly? Are there any other strategies?

Community
  • 1
  • 1
Mark
  • 7,507
  • 12
  • 52
  • 88

3 Answers3

1

Two things here:

  1. You missed "project" during the import. Use github.com/GITHUB_USERNAME/PROJECTNAME/lib/model
  2. Minor: Naming the package github.com/GITHUB_USERNAME/PROJECTNAME/lib/model "PROJECTNAME" seems strange. How about lib or model?
Volker
  • 40,468
  • 7
  • 81
  • 87
  • Thanks for your help! 1. Sorry that was my fault. I have tried a lot of combinations and copied the wrong output ;) I have replaced it with the current output 2. The ```model``` should be a private file, that won't be used in other projects. – Mark Feb 12 '14 at 16:46
  • 1
    There is no notion of a "private package" in Go. All you can do is "not use" it elsewhere, but this cannot be enforced. – Volker Feb 12 '14 at 17:58
  • I meant quote:```if they are not intended to be imported by another project``` – Mark Feb 12 '14 at 18:06
1

Your import statement from your main package should read

import(
    "github.com/GITHUB_USERNAME/PROJECTNAME/lib/PROJECTNAME"
) 

You said your model.go uses PROJECTNAME as the package. So you don't actually name a file in your import path. The last component should be the package name. That means you can have a number of go files in the lib directory all with PROJECTNAME as the package.

jdi
  • 90,542
  • 19
  • 167
  • 203
1

Package import paths are defined by directory names, not file names.

The correct import path is "github.com/GITHUB_USERNAME/PROJECTNAME/lib" and all of the go files in that folder must have the same package clause at the top.

The identifier after the package clause is what identifier will be imported into the package that imports it. So if it's package foo, you can access the Bar identifier by foo.Bar in the importing code.

By convention, authors typically use the last part of the import path as the package name, so in this case, you should have package lib at the top of all of the Go files under the lib folder.

zeebo
  • 96
  • 1
  • Hi. I'm not sure the wording is correct when you say the import path is fully based on folders. From the docs "Go's convention is that the package name is the last element of the import path". So the containing folder doesn't have to match the package name necessarily. – jdi Feb 13 '14 at 01:20
  • The import path _is_ fully defined by the folder. That doesn't have anything to do with what _package_ is defined by the go files _inside_ of that folder. Import path is totally unrelated to package name as you have noticed. Can you point out more specifically what I said that caused the confusion? – zeebo Feb 13 '14 at 19:17
  • I think I understand what you're claiming better, but consider the package `github.com/golang/glog`. The file `glog.go` would exist as `$GOPATH/src/github.com/golang/glog/glog.go` and declare `package glog`. You would import this with `import "github.com/golang/glog"`, not `import "github.com/golang/glog/glog"` even though `glog` is the package. If `glog.go` declared `package foo`, you would still import it the same way, but the identifier imported would be `foo` instead of `glog`. Does that make it more clear? – zeebo Feb 13 '14 at 19:34
  • Yep. I think it was just the wording. – jdi Feb 13 '14 at 21:04