How do I import local packages using the Go Module

Go Module is the official version management tool released after GO 1.11, and starting with GO 1.13, Go Module will be the default dependency management tool for the GO language. As of today, Go Modules is officially recommended for use in production with the release of Go1.17.

There have been a number of tutorials over the last few days explaining how to use the Go Module and how to import the GitLab private repository using the Go Module, so I won’t be bothering you here. But recently, I found that many friends in the group asked how to use go Module to import local packages. As beginners, when they first touch package, they must first create a package locally, then call it locally, and then get stuck.

Here’s how to use the Go Module to import local packages.

The premise

Suppose we now have the ModuleDemo and mypackage packages, where the ModuleDemo package imports the mypackage package and uses its New method.

The contents of mypackage/mypackage.go are as follows:

package mypackage

import "fmt"

func New(){
	fmt.Println("mypackage.New")
}
Copy the code

Under the same project

Note: It is possible to define multiple packages under a project.

The directory structure

In this case, we called the mypackage package in moduledemo/main.go.

├── ├─ exercises, exercises, exercises, exercises, exercises, exercisesCopy the code

Import packages

In this case, we need to define moduledemo/go.mod as follows:

The module moduledemo go 1.17Copy the code

Then import mypackage in moduledemo/main.go as follows

Package main import (" FMT ""moduledemo/mypackage" // import the same project mypackage package) func main() {mypackage.new () fmt.Println("main") }Copy the code

For example

By analogy, suppose we now have a file directory structure as follows:

└ ─ ─ mercifully ├ ─ ─ dao │ └ ─ ─ mysql. Go ├ ─ ─. Mod └ ─ ─ main. GoCopy the code

Bubble /go.mod contains the following contents:

The module github.com/q1mi/bubble go 1.17Copy the code

Bubble /dao/mysql.go contains the following contents:

package dao

import "fmt"

func New(){
	fmt.Println("mypackage.New")
}
Copy the code

The contents of bubble/main.go are as follows

package main

import (
	"fmt"
	"github.com/q1mi/bubble/dao"
)
func main() {
	dao.New()
	fmt.Println("main")
}
Copy the code

Not under the same project

The directory structure

├── ├─ ├─ go, go, go, go, go, go, go, go, go, go, go, go, go, goCopy the code

Import packages

At this point, mypackage also needs to initialize the Module, that is, to have its own go.mod file, which reads as follows:

The module mypackage go 1.17Copy the code

Then we import it in moduledemo/main.go as follows:

import (
	"fmt"
	"mypackage"
)
func main() {
	mypackage.New()
	fmt.Println("main")
}
Copy the code

Because the two packages are not in the same project path, you want to import the local packages, and the packages are not published to a remote Github or other repository address. At this point we need to use the replace directive in the go.mod file.

In the caller, moduledemo/go.mod, specify the relative path to find the mypackage package as follows.

Module Moduledemo go 1.17 require "mypackage" v0.0.0 replace "mypackage" => ".. /mypackage"Copy the code

For example

Finally, let’s take another example to reinforce the above.

We now have the following file directory structure:

├ ─ ─ p1 │ ├ ─ ─. Mod │ └ ─ ─ main. Go └ ─ ─ p2 ├ ─ ─. Mod └ ─ ─ p2. GoCopy the code

P1 /main.go wants to import functions defined in p2.go.

P2 /go.mod reads as follows:

The module liunian. Cn/q1mi/p2 go 1.17Copy the code

Go to import the file in the following way

import (
	"fmt"
	"liunian.cn/q1mi/p2"
)
func main() {
	p2.New()
	fmt.Println("main")
}
Copy the code

Because I did not upload the liunian.cn/q1mi/p2 package to the website liunian.cn, we just want to import the local package, at this time, we need to use the replace command.

The contents of p1/go.mod are as follows:

Module github.com/q1mi/p1 go 1.17 require "liunian.cn/q1mi/p2" v0.0.0 replace "liunian.cn/q1mi/p2" => ".. /p2"Copy the code

At this point, we can compile the PROJECT P1 normally.