作者:ReganYue

起源:恒生LIGHT云社区

一、应用 go get获取

GO的命令 go get让咱们能够方便快捷的从网络中下载或更新Go语言包及其依赖文件,并将他们编译和装置。

  1. 先在命令行模式下输出go --help,可查看以下信息。
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 packagesUse "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 functionsUse "go help <topic>" for more information about that topic.

咱们能够看到 get add dependencies to current module and install them

这就是咱们明天要用到的货色。

而后查看它的帮忙信息:go get --help

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

找到咱们要获取的库。

复制链接 https://github.com/go-sql-driver/mysql

应用 go get github.com/go-sql-driver/mysql

后果报错:

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

这里要装置Git,博主是装置了Git的,不过这里须要配置一下。

在零碎变量的PATH外面增加Git的bin目录的门路。

而后再运行 go get github.com/go-sql-driver/mysql

当初就胜利了。

这个 github.com/go-sql-driver/mysql被称为近程导入门路,其实go get被称为近程动静导入GO包。其实 go get干的事就是从分布式版本控制系统的仓库中找出代码包并将它编译以及装置。

仓库名VCSVCSVCS
BitBucketMercurialGit
GitHubGit
Google Code Project HostingGitMercurialSubversion
LaunchpadBazaar

个别状况下,代码包近程导入门路中的第一个元素就是代码托管网站的主域名。在动态剖析的时候,go get命令会将代码包近程导入门路与预置的代码托管站点的主域名进行匹配。如果匹配胜利,则在对代码包近程导入门路的初步查看后返回失常的返回值或错误信息。如果匹配不胜利,则会再对代码包近程导入门路进行动态分析。

二、间接下载GO库

在 github 或者其余中央下载Go库。

而后在零碎变量查看GOPATH。

而后关上GOPATH的src目录

将文件复制进去。

而后就胜利获取了。

三、参数介绍

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

而后咱们能够看一看 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 thateach package has been checked out from the source control repositoryimplied by its import path. This can be useful if the source is a local forkof the original.The -fix flag instructs get to run the fix tool on the downloaded packagesbefore resolving dependencies or building the code.The -insecure flag permits fetching from repositories and resolvingcustom domains using insecure schemes such as HTTP. Use with caution.The -t flag instructs get to also download the packages required to buildthe tests for the specified packages.The -u flag instructs get to use the network to update the named packagesand their dependencies. By default, get uses the network to check outmissing packages but does not use it to look for updates to existing packages.The -v flag enables verbose progress and debug output.

简略来说就是:

  • -d 下载实现后就进行工作,不装置库
  • -f 这个参数只有在应用了-u 参数时才有用,强制-u不去验证import的每一个包是否曾经获取了,这对本地fork的包十分有用。
  • -fix 这个-fix参数示意在解决依赖关系或构建代码之前先运行fix工具。
  • -insecure 该参数容许通过不平安(例如 HTTP)的自定义域获取并解析应用存储库。
  • -t 该参数容许在下载该包时也下载测试该包须要的包。
  • -u 参数容许 get 应用网络来更新包以及它们的依赖。默认状况下,get 应用网络查看失落的包,但不应用它来查找现有包的更新。
  • -v 启用具体进度和调试信息输入。