此篇属于golang开发基础、入门,管理源码中包的依赖关系、版本。捋顺包管理方法、重要性不言而喻。
在 go1.11 之前,安装 golang 之后,需要配置两个环境变量即GOROOT 和GOPATH。
GOROOT是 go 安装后的所在的路径,GOPATH是开发工作目录路径,用于存放go 源代码的地方。
在 GOPATH 路径内,有三个文件夹,分别是:
建立工作环境
robot@ubuntu:~$ mkdir -p golang/workspace1.15
开发的程序源码放在src里,可以在src里创建多个项目,每一个项目同时也是一个文件夹。
如下所示,
robot@ubuntu:~/golang/workspace1.15$ go version
go version go1.11 linux/amd64
robot@ubuntu:~/golang/workspace1.15$ echo $GOPATH
/home/robot/golang/go-workspace
robot@ubuntu:~/golang/workspace1.15$ ls
hello.go user
二、go module 使用方法
2.1 go mod init 初始化包管理名
robot@ubuntu:~/golang/workspace1.15$go mod init golang11
robot@ubuntu:~/golang/workspace1.15$cat go.mod
module golang11
require github.com/gin-gonic/gin v1.7.2
2.2 go mod tidy 下载源码依赖包
robot@ubuntu:~/golang/workspace1.15$ go mod tidy
go: finding github.com/gin-gonic/gin v1.7.2
go: downloading github.com/gin-gonic/gin v1.7.2
go: finding github.com/golang/protobuf v1.3.3
go: finding github.com/stretchr/testify v1.4.0
go: finding github.com/ugorji/go/codec v1.1.7
go: finding github.com/mattn/go-isatty v0.0.12
go: finding github.com/json-iterator/go v1.1.9
go: finding github.com/gin-contrib/sse v0.1.0
go: finding github.com/go-playground/validator/v10 v10.4.1
go: finding gopkg.in/yaml.v2 v2.2.8
go: finding github.com/stretchr/testify v1.3.0
go: finding golang.org/x/sys v0.0.0-20200116001909-b77594299b42
go: finding github.com/google/gofuzz v1.0.0
go: finding github.com/davecgh/go-spew v1.1.1
go: finding github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: finding github.com/leodido/go-urn v1.2.0
go: finding github.com/go-playground/assert/v2 v2.0.1
go: finding github.com/go-playground/locales v0.13.0
go: finding github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: finding gopkg.in/yaml.v2 v2.2.2
go: finding github.com/davecgh/go-spew v1.1.0
go: finding golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: finding github.com/ugorji/go v1.1.7
go: finding github.com/stretchr/objx v0.1.0
go: finding github.com/pmezard/go-difflib v1.0.0
go: finding github.com/go-playground/universal-translator v0.17.0
go: finding gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: finding golang.org/x/sys v0.0.0-20190412213103-97732733099d
go: finding golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
go: finding golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
go: finding golang.org/x/text v0.3.0
go: finding golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
go: finding golang.org/x/text v0.3.2
go: finding golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/ugorji/go v1.1.7
go: downloading github.com/json-iterator/go v1.1.9
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/go-playground/validator/v10 v10.4.1
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/stretchr/testify v1.4.0
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading golang.org/x/sys v0.0.0-20200116001909-b77594299b42
go: downloading github.com/ugorji/go/codec v1.1.7
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: downloading github.com/leodido/go-urn v1.2.0
go: downloading github.com/go-playground/assert/v2 v2.0.1
go: downloading github.com/go-playground/locales v0.13.0
go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: downloading github.com/go-playground/universal-translator v0.17.0
查看下载内容
robot@ubuntu:~/golang/workspace1.15$ tree ../go-workspace/pkg/mod/ -L 2
../go-workspace/pkg/mod/
├── cache
│ ├── download
│ └── vcs
├── github.com
│ ├── davecgh
│ ├── gin-contrib
│ ├── gin-gonic
│ ├── golang
│ ├── google
│ ├── go-playground
│ ├── json-iterator
│ ├── leodido
│ ├── mattn
│ ├── modern-go
│ ├── pmezard
│ ├── stretchr
│ └── ugorji
├── golang.org
│ └── x
└── gopkg.in
├── check.v1@v0.0.0-20161208181325-20d25e280405
└── yaml.v2@v2.2.8
2.3 go mod vendor 下载包至本地vendor
会在项目中建立vendor目录,将依赖下载在该目录中,目录结构类似前面说的的pkg/mod目录,整个项目打包的话方便没有网络也可以编译项目。
robot@ubuntu:~/golang/workspace1.15$ go mod vendor
robot@ubuntu:~/golang/workspace1.15$ ls vendor/
github.com golang.org gopkg.in modules.txt
2.4 go mod download 下载包至 GOPATH的pkg文件夹
都下载到了$GOPATH/pkg/mod目录,与go mod tidy 具体差别本人没有深入对比。
2.5 go mod help查看帮助内容如下
robot@ubuntu:~/golang/workspace15$ go mod help
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
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
Use "go help mod " for more information about a command.
三、项目编译
3.1 go build 源码
版本依赖管理实例:
robot@ubuntu:~/golang/workspace1.15$ go build hello.go
go build github.com/gin-gonic/gin/internal/bytesconv: module requires Go 1.13
go build github.com/go-playground/locales/currency: module requires Go 1.13
go build golang.org/x/sys/unix: module requires Go 1.12
go build github.com/gin-gonic/gin/internal/json: module requires Go 1.13
go build github.com/leodido/go-urn: module requires Go 1.13
go build github.com/golang/protobuf/proto: module requires Go 1.12
go build github.com/gin-contrib/sse: module requires Go 1.12
robot@ubuntu:~/golang/workspace1.15$
3.2 本地目录源码管理
建立本地user文件,并建立文件user.go文件
内容如下:
package user
import( "fmt" )
func User(){
fmt.Println("sub forlder of project \n")
}
入口函数 main.go中调用方法如下
package main
import (
"golang11/user" // golang11 是 go mod init 设置module名称
"log"
"github.com/gin-gonic/gin"
"net/http"
)
type User struct {
Name string `json:"name"`
Password int64 `json:"password"`
}
func main() {
user.User()
r := gin.Default()
r.GET("/welcome",func(c *gin.Context){
json := make(map[string]interface{})
c.BindJSON(&json)
log.Printf("%v",&json)
c.JSON(http.StatusOK, gin.H{
"name":json["name"],
"password":json["password"],
})
/*
json := User{}
c.BindJSON(&json)
log.Println("%v",&json)
c.JSON(http.StatusOK, gin.H{
"name":json.Name,
"password":json.Password,
})
*/
})
r.Run(":8088") //listen 8088 port
}
参考链接:
https://golang.google.cn/doc/modules/developing
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)