How to import third-party libraries of Go language

Go get = go get

The GO get command allows you to quickly and easily download or update the GO language package and its dependency files from the network, compile and install them.

Enter go –help in the command line interface (CLI) to view the following information:

Go is a tool for managing Go source code.
​
Usage:
​
        go <command> [arguments]
​
The commands are:
​
        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages
​
Use "go help <command>" for more information about a command.
​
Additional help topics:
​
        buildconstraint build constraints
        buildmode       build modes
        c               calling between Go and C
        cache           build and test caching
        environment     environment variables
        filetype        file types
        go.mod          the go.mod file
        gopath          GOPATH environment variable
        gopath-get      legacy GOPATH go get
        goproxy         module proxy protocol
        importpath      import path syntax
        modules         modules, module versions, and more
        module-get      module-aware go get
        module-auth     module authentication using go.sum
        module-private  module configuration for non-public modules
        packages        package lists and patterns
        testflag        testing flags
        testfunc        testing functions

Use "go help <topic>" for more information about that topic.
Copy the code

See get add dependencies to current Module and install them

That’s what we’re going to use today.

Then check its help information: go get –help

usage: go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
Run 'go help get' for details.
Copy the code

Find the library we want to retrieve.

Copy the link to https://github.com/go-sql-driver/mysql

Use go get github.com/go-sql-driver/mysql

Error:

go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/go-sql-driver/mysql: exec: "git": executable file not found in %PATH%
Copy the code

Git is installed here. The blogger has Git installed, but it needs to be configured.

Add Git bin directory PATH to system variable PATH.

Then run go get github.com/go-sql-driver/mysql

Now it works.

This github.com/go-sql-driver/mysql is called the remote import path, but actually go GET is called the remote dynamic import go package. Essentially, what Go Get does is find the code package from the repository of the distributed version control system and compile and install it.

Warehouse, VCS VCS VCS
BitBucket Mercurial Git
GitHub Git
Google Code Project Hosting Git Mercurial Subversion
Launchpad Bazaar

Typically, the first element in the code package remote import path is the primary domain name of the code hosting site. During static analysis, the go get command matches the remote import path of the code package to the pre-defined primary domain name of the code hosting site. If the match is successful, a normal return value or error message is returned after an initial check of the remote import path of the code package. If the match is unsuccessful, the code package remote import path is dynamically analyzed again.

2. Download the GO library directly

Download the Go library on Github or somewhere else.

Then look at GOPATH in system variables.

Then open the GOPATH SRC directory

Copy the file in.

And then you get it.

3. Parameter introduction

usage: go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
Run 'go help get' for details.
Copy the code

Then we can take a look at the parameters in go Help Get.

The -d flag instructs get to stop after downloading the packages; that is,
it instructs get not to install the packages.
​
The -f flag, valid only when -u is set, forces get -u not to verify that
each package has been checked out from the source control repository
implied by its import path. This can be useful if the source is a local fork
of the original.
​
The -fix flag instructs get to run the fix tool on the downloaded packages
before resolving dependencies or building the code.
​
The -insecure flag permits fetching from repositories and resolving
custom domains using insecure schemes such as HTTP. Use with caution.
​
The -t flag instructs get to also download the packages required to build
the tests for the specified packages.
​
The -u flag instructs get to use the network to update the named packages
and their dependencies. By default, get uses the network to check out
missing packages but does not use it to look for updates to existing packages.
​
The -v flag enables verbose progress and debug output.
Copy the code

To put it simply:

  • -d stops working when the download is complete and does not install the library
  • The -f parameter is only useful if the -u parameter is used. Forcing -u not to verify that every package imported has been fetched is useful for locally forked packages.
  • -fix The -fix parameter means to run the fix tool before resolving dependencies or building code.
  • -insecure This parameter allows the repository to be obtained and parsed through a custom domain that is not secure (such as HTTP).
  • -t This parameter allows the package to be downloaded at the same time as the package to be tested.
  • The -u parameter allows GET to use the network to update packages and their dependencies. By default, GET uses the network to check for lost packets, but does not use it to find updates to existing packets.
  • -v Enables the detailed progress and debugging information output.