go module Example of use

go languages, from 1.11, the introduction module, version management.

By using the module, the directory location of the project does not have to be placed under GOPATH.

This article describes the use of the module.

Go with the following version is 1.13.

1. go mod command

Related operations can be performed by modules go mod.

First look go mod command:

$ go help mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

    go mod <command> [arguments]

The commands are:

    download    download modules to local cache   // 下载依赖的module到本地cache
    edit        edit go.mod from tools or scripts
    graph       print module requirement graph
    init        initialize new module in current directory  // 在当前目录下初始化新的module
    tidy        add missing and remove unused modules  // 添加缺失的module,移出不用的module
    vendor      make vendored copy of dependencies  // 将依赖复制到vendor下
    verify      verify dependencies have expected content
    why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

Example 2. Use module

2.1 Definitions module

Create a project directory in any directory, you can not GOPATH of the path.

$ mkdir hello_mysql
$ pwd
/home/lanyang/workspace/go_example/hello_mysql

$ go mod init hello_mysql
go: creating new go.mod: module hello_mysql

$ ll
-rw-rw-r--. 1 lanyang lanyang  28 Dec 21 18:25 go.mod

$ cat go.mod
module hello_mysql

go 1.13

New source files main.go, as follows:

// main.go

package main

import (
        "database/sql"
        "log"

        _ "github.com/go-sql-driver/mysql"

)

var DB *sql.DB
var dataBase = "root:Aa123456@tcp(192.168.0.101:3306)/?timeout=5s&readTimeout=6s"

func mysqlInit() {
        var err error
        DB, err = sql.Open("mysql", dataBase)
        if err != nil {
                log.Fatalln("open db fail:", err)
        }

}

func main() {
        mysqlInit()

        execSql()
}

func execSql() {
        var value int
        err := DB.QueryRow("select 1").Scan(&value)
        if err != nil {
                log.Println("query failed:", err)
                return
        }

        log.Println("value:", value)
}

The above code to achieve connect to the MySQL server, execute simple SQL statements.

2.2 compiler

$ go build
go: downloading github.com/go-sql-driver/mysql v1.4.1
go: extracting github.com/go-sql-driver/mysql v1.4.1
go: finding github.com/go-sql-driver/mysql v1.4.1

$ ll
total 5788
-rw-rw-r--. 1 lanyang lanyang      75 Dec 22 16:57 go.mod
-rw-rw-r--. 1 lanyang lanyang     179 Dec 22 16:57 go.sum
-rwxrwxr-x. 1 lanyang lanyang 5912033 Dec 22 16:58 hello_mysql
-rw-rw-r--. 1 lanyang lanyang     669 Dec 22 16:57 main.go

In Go 1.13, the continued use of temporary environment variable GO111MODULE to set whether to use the module.
GO111MODULE can be set off, on, or auto (default).

If GO111MODULE = auto or not set, go command (ie go build, go test go get, etc.) whether the module depends on the existence go.mod file.
If there go.mod files in the current directory, or the parent directory, then go command will use the module, you would not use the module.

Example, is not provided GO111MODULE, default values.

By defining module, we have created the go.modfile, so that execution go command and they will use the module to find and download dependencies and dependencies add information to the go.modfile.

Download depends on the package $GOPATH/pkg/mod.

$ ll $GOPATH/pkg/mod/
total 8
drwxrwxr-x. 3 lanyang lanyang 4096 Dec 22 16:58 cache
drwxrwxr-x. 3 lanyang lanyang 4096 Dec 22 16:58 github.com

go.modContents of the file is modified as follows:

module hello_mysql

go 1.13

require github.com/go-sql-driver/mysql v1.4.1

After compilation, create an executable file hello_mysql, execute:

$ ./hello_mysql 
2019/12/22 17:19:19 value: 1

3.module 与 vendor

For compatibility with older versions, or want all the dependencies in a directory, you can use go mod vendor.

go mod vendorCreated at the root vendordirectory, which stores all of the dependencies.

Compile time, plus options -mod=vendor, you can use vendordependencies directory.

Continue Using the above example.

Cleanup pkg / mod:

$ rm -rf $GOPATH/pkd/mod

Under the example code directory, retaining only go.modand main.go.

Using the stored vendor dependencies, execute go mod vendor:

$ go mod vendor
go: downloading github.com/go-sql-driver/mysql v1.4.1
go: extracting github.com/go-sql-driver/mysql v1.4.1
go: finding google.golang.org/appengine v1.6.5
go: downloading google.golang.org/appengine v1.6.5
go: extracting google.golang.org/appengine v1.6.5

$ ll
total 16
-rw-rw-r--. 1 lanyang lanyang  128 Dec 22 17:23 go.mod
-rw-rw-r--. 1 lanyang lanyang 1034 Dec 22 17:23 go.sum
-rw-rw-r--. 1 lanyang lanyang  669 Dec 22 17:19 main.go
drwxrwxr-x. 4 lanyang lanyang 4096 Dec 22 17:23 vendor

vendorDirectory and go.sumfile is created.

View go.modfiles, the content is amended as follows:

module hello_mysql

go 1.13

require (
        github.com/go-sql-driver/mysql v1.4.1
        google.golang.org/appengine v1.6.5 // indirect
)

View the next vendorcontents of the directory:

$ ll vendor/
total 12
drwxrwxr-x. 3 lanyang lanyang 4096 Dec 22 17:23 github.com
drwxrwxr-x. 3 lanyang lanyang 4096 Dec 22 17:23 google.golang.org
-rw-rw-r--. 1 lanyang lanyang  145 Dec 22 17:23 modules.txt

You can see the store with all the dependencies directory.

Compile-time, adding -mod=vendor:

$ go build -mod=vendor
$ ll
total 5792
-rw-rw-r--. 1 lanyang lanyang     128 Dec 22 17:23 go.mod
-rw-rw-r--. 1 lanyang lanyang    1034 Dec 22 17:23 go.sum
-rwxrwxr-x. 1 lanyang lanyang 5912033 Dec 22 17:34 hello_mysql
-rw-rw-r--. 1 lanyang lanyang     669 Dec 22 17:19 main.go
drwxrwxr-x. 4 lanyang lanyang    4096 Dec 22 17:23 vendor

In this case, the vendor will look for dependencies in.

carried out:

$ ./hello_mysql 
2019/12/22 17:36:40 value: 1

In this way, both the use of the module features, get rid of dependence on GOPATH project directory, but also the use of vendor storage dependencies.

Especially in the pre-compiled and packaged on-line stage, dependencies stored in advance in the vendor directory, you can no longer go to download through the network, to avoid the impact of network instability, which can save a lot of time.

4. Reference

About go module

go mod use

Go Module Getting Started

Guess you like

Origin www.cnblogs.com/lanyangsh/p/12080482.html