I often get hooked when importing in a place other than $ GOPATH
.
That is because there are the following patterns.
For each of these, sort out how import is different. (GO version is assumed to be 1.14)
When I write the outline of the conclusion, it looks like the following
$ GOPATH / src
$ GOPATH / pkg / mod
GOPATH mode and module mode switch depending on the presence or absence of the go.mod
file
go.mod
file missinggo.mod
file (** It is valid even if there is go.mod in the hierarchy above the directory to be executed **)Directory structure
.
└── main.go
main.go
package main
import (
"fmt"
"github.com/antonholmquist/jason" //Importing external packages
)
func main() {
v, _ := jason.NewObjectFromBytes([]byte(`{"Name": "fetaro"}`))
fmt.Println(v)
}
$GOPATH/src/
Specifically, $ GOPATH / src / github.com/antonholmquist/jason
cannot find package
error
main.go:5:2: cannot find package "github.com/antonholmquist/jason" in any of:
/usr/local/Cellar/go/1.14.3/libexec/src/github.com/antonholmquist/jason (from $GOROOT)
/Users/tetsutaro.watanabe/go/src/github.com/antonholmquist/jason (from $GOPATH)
go get
Specifically, go get github.com/antonholmquist/jason
Directory structure
.
├── go.mod ←go.I put the mod file, so it goes into module mode
└── main.go
main.go
package main
import (
"fmt"
"github.com/antonholmquist/jason" //Importing external packages
)
func main() {
v, _ := jason.NewObjectFromBytes([]byte(`{"Name": "fetaro"}`))
fmt.Println(v)
}
go.mod
module hoge
go 1.14
$GOPATH/pkg/mod
Specifically, /Users/tetsutaro.watanabe/go/pkg/mod/github.com/antonholmquist/jason\@v1.0.0/
It will be downloaded without permission.
If you do go run main.go
while it has not been downloaded yet, it will be downloaded and then executed as shown below.
$ go run main.go
go: finding module for package github.com/antonholmquist/jason
go: found github.com/antonholmquist/jason in github.com/antonholmquist/jason v1.0.0 ← Download result
{"Name":"fetaro"}← Execution result
Once downloaded, go.mod
will be rewritten
Rewritten go.mod
module hoge
go 1.14
require github.com/antonholmquist/jason v1.0.0 // indirect
Directory structure
.
├── main.go
└── mypkg ← Internal package
└── bar.go
main.go
package main
import (
"./mypkg" //Import internal packages
)
func main() {
mypkg.PrintBar()
}
bar.go
package mypkg
import "fmt"
func PrintBar(){
fmt.Println("bar")
}
./mypkg
with a relative path from main.go
cannot find package
error
main.go:4:2: cannot find package "." in:
/Users/tetsutaro.watanabe/git/golab3/mypkg
Directory structure
.
├── go.mod ←go.Module mode because there is a mod
├── main.go
└── mypkg ← Internal library
└── bar.go
main.go
package main
import (
"mymodule/mypkg" //← go here.Module name written in mod+Make it a package name
)
func main() {
mypkg.PrintBar()
}
go.mod
module mymodule
go 1.14
bar.go
package mypkg
import "fmt"
func PrintBar(){
fmt.Println("bar")
}
This is difficult.
In module mode, " mymodule / mypkg "
specified in import is a concatenation of module and package.
Specifically, you'll be looking for (module name in go.mod) + (relative path to the package from where go.mod is).
In this example, mymodule
is the module and / mypkg
is the relative path from the directory containing "go.mod with module mymodule".
Get an error
main.go:4:2: package mymodule/mypkg is not in GOROOT (/usr/local/Cellar/go/1.14.3/libexec/src/mymodule/mypkg)
Case where go.mod is higher than main.go
For example, in the case of the following directory structure
.
├── c_pkg
│ ├── d_pkg
│ │ └── bar.go
│ └── main.go
└── go.mod
If you specify module a_module / b_module
in go.mod,
In main.go, it can be read by import" a_module / b_module / c_pkg / d_pkg "
Common cmd and internal configurations
.
├── cmd
│ └── main.go
├── go.mod
└── internal
└── lib.go
go.mod
module github.com/fetaro/my_module
go 1.14
main.go
package main
import (
"github.com/fetaro/my_module/internal"
)
func main() {
internal.PrintHoge()
}
lib.go
package internal
import "fmt"
func PrintHoge(){
fmt.Println("hoge")
}
Recommended Posts