A detailed tutorial on importing native packages using go module

  • 2020-10-07 18:43:21
  • OfStack

go module is the official version management tool after Go1.11, and starting with Go1.13, go module will be the default dependency management tool for the Go language. Today, the Go modules feature has been officially recommended for use in production since version 1.14 of Go was released.

There have been many tutorials over the last few days on how to use go module and how to use go module to import into the gitlab private repository, and I won't bother here. But recently I found that many friends asked how to use go module to import local packages in the group. As a beginner, when you start to learn package, you must first create a package locally, then call 1 locally, and then get stuck...

Here's how to import local packages using go module in more detail.

The premise

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

mypackage/ ES37en. go reads as follows:


package mypackage
import "fmt"
func New(){
 fmt.Println("mypackage.New")
}

Let's discuss it in two ways:

Under the same item

Note: It is possible to define multiple packages (package) under one project (project).

The directory structure

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


moduledemo
 ├ ─ ─  go.mod
 ├ ─ ─  main.go
 └ ─ ─  mypackage
   └ ─ ─  mypackage.go

Import packages

At this point, we need to define moduledemo/ ES62en.mod as follows:

[

module moduledemo

go 1.14

]

Then import mypackage in moduledemo/ main.go as follows


package main
import (
 "fmt"
 "moduledemo/mypackage" //  The import with 1 Under the project of mypackage package 
)
func main() {
 mypackage.New()
 fmt.Println("main")
}

For example

For example, 1 to 3, suppose we now have the following file directory structure:

[

└ ─ ─ bubble
├ ─ ─ dao
│ └ ─ ─ mysql go
├ ─ ─ go mod
└ ─ ─ main go

]

bubble/ ES102en. mod is as follows:


module github.com/q1mi/bubble

go 1.14

bubble dao/mysql. go content is as follows:


package dao
import "fmt"
func New(){
 fmt.Println("mypackage.New")
}

bubble/main.go is as follows:


package main

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

Not under the same item

The directory structure

[

├ ─ ─ moduledemo
│ ├ ─ ─ go mod
│ └ ─ ─ main go
└ ─ ─ mypackage
├ ─ ─ go mod
└ ─ ─ mypackage go

]

Import packages

At this time, mypackage also needs to initialize module, that is, it has its own ES147en. mod file, which is as follows:

[

module mypackage

go 1.14

]

Then we import in moduledemo/ ES160en.go as follows:


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

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

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


module moduledemo
go 1.14
require "mypackage" v0.0.0
replace "mypackage" => "../mypackage"

For example

Finally, let's do one more example to reinforce the above.

We now have the file directory structure as follows:

[

├ ─ ─ p1
│ ├ ─ ─ go mod
│ └ ─ ─ main go
└ ─ ─ p2
├ ─ ─ go mod
└ ─ ─ p2 go

]

p1/ main.go wants to import the functions defined in ES206en2.go.

p2/ ES211en. mod:

[

module liwenzhou.com/q1mi/p2

go 1.14

]

p1/ ES224en.go import as follows


import (
 "fmt"
 "liwenzhou.com/q1mi/p2"
)
func main() {
 p2.New()
 fmt.Println("main")
}

Because I didn't take the liwenzhou com q1mi/p2 this package uploaded to liwenzhou. com this website, we just want to import the local package, this time you need to use replace this instruction.

p1/go.mod reads as follows:

[

module github.com/q1mi/p1
go 1.14
require "liwenzhou.com/q1mi/p2" v0.0.0
replace "liwenzhou.com/q1mi/p2" = > "../p2"

]

At this point, we are ready to compile the p1 project as normal.

It's no use talking too much. Try it yourself.

conclusion


Related articles: