I do understand Go is quirky for imports, but I've tried following the convention (I believe) to the point, however I cannot import a struct.
Project structure:
/project-name
/parser/main.go
/query/main.go
... Project files in root
I have a struct in /parser/main.go
exported:
package parser
type SomeTranslationStuff struct {
ID int `json:"Id"`
Language string `json:"Language"`
}
I wish to import this in /query/main.go
.
I do it as such:
import (
"github.com/org/project-name/parser"
)
It does not import it - I am met with "cannot find package ... in Gopath".
My project exists under: Users\user\go\src\project-name
. The imported project(which is the same project, imported from github via go get) does indeed exist in Users\user\go\src\github.com\org\project-name
.. Everything seems according to the "Go"-way of importing stuff, yet it doesn't appear to import?
It specifically says:
Cannot find package name:
C:\Users\user\go\src\github.com\org\project-name\parser(from $GOPATH)
If I go to that path, the project is, correctly, there! What gives? Why doesn't it import?
A package has two things:
package thename
declaration.You should (no arguing here) do the following:
The name and the last component of the import path should match. E.g. if the import path is "sun/moon/stars" than the package name is "stars". (A lot of people get this wrong and either call the package or the folder stuff like go-stars. Don't do that.)
Your folder structure on disk must match the import paths of your package. If the import path of your package is "whatever/random/noise" you must place it under $GOPATH/src/whatever/random/noise. Or put the other way around: If you code is in $GOPATH/src/ill/do/it/my/own/way then the import path of package "way" is "ill/do/it/my/own/way".
If you want you package to be go get
able place it on a known code hosting and import it by the full import path. Restrictions on import path might apply. Place code of package "foo" under $GOPATH/src/github.com/you/repo/whatever/deep/folder/structure/foo and import it with import "github.com/you/repo/whatever/deep/folder/structure/foo"
All your paths and package names should be all lowercase.
It boils down to: Do not do anything fancy, keep it natural. Import path and folder structure must match.