Module
- Module is a collection of packages.
- It is better to use the module name in
Domain/Module format. - The module naming convention is lowercase and single-word.
It should be declared in the top of go.mod file.
1
2
3
| module cozyfex.com/sample1
go 1.20
|
GOPATH
It is recommended that the package name of Root Path of the module be the same as Module.
Package
- The package name is the directory’s name that the file exist.
- It should be same package name for files that same path.
- The package naming convention is lowercase and single-word.
Import
- It is possible to
import per package. - Even a
package under the same project is imported as a package under module declared in go.mod. - For using external package it should be declared in
go.mod file. - The
require of go.mod file is finding the module in GOPATH. - Modules that cannot be installed with
go get can be imported using require and replace.
Importing local module
Suppose there are two modules below.
Scenario
This is an example of trying to use the packages of the sample1 module in sample2.
Both sample1 and sample2 are located locally, and sample1 cannot be installed with go get.
Solution
In the go.mod file of sample2, replace the path of the sample1 module to a place other than GOPATH.
Among the long articles above, the most important points are the following.
1
2
| require cozyfex.com/sample1 v0.0.0
replace cozyfex.com/sample1 v0.0.0 => ../sample1
|
As with other modules, require it, then use replace to change the path.
In this case, both absolute and relative paths can be used for the path.
cozyfex.com/sample1
File Structure
1
2
3
4
5
6
| │ go.mod
│ what.go
│ sub
│ who.go
└─ directory
│ where.go
|
File Contents
go.mod
1
2
3
| module cozyfex.com/sample1
go 1.20
|
what.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| package sample1
import (
"fmt"
"cozyfex.com/sample1/sub"
"cozyfex.com/sample1/sub/directory"
)
func Name() {
sub.Who()
directory.Where()
fmt.Println("This is sample1 module")
}
func What() {
fmt.Println("What")
}
|
As you can see here, when you import a package in the same project, it imports based on the module name declared in
the go.mod file.
sub/who.go
1
2
3
4
5
6
7
8
9
| package sub
import (
"fmt"
)
func Who() {
fmt.Println("Who")
}
|
sub/directory/where.go
1
2
3
4
5
6
7
8
9
| package directory
import (
"fmt"
)
func Where() {
fmt.Println("Where")
}
|
cozyfex.com/sample2
File Structure
File Contents
go.mod
1
2
3
4
5
6
7
| module cozyfex.com/sample2
go 1.20
require cozyfex.com/sample1 v0.0.0
replace cozyfex.com/sample1 v0.0.0 => ../sample1
|
To import a local module, require and replace are used to match the module name with the path.
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package main
import (
"cozyfex.com/sample1"
"cozyfex.com/sample1/sub"
"cozyfex.com/sample1/sub/directory"
)
func main() {
sample1.Name()
sample1.What()
sub.Who()
directory.Where()
}
|