Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang: Go 1.11 Modules #113

Open
hhstore opened this issue Sep 23, 2018 · 4 comments
Open

Golang: Go 1.11 Modules #113

hhstore opened this issue Sep 23, 2018 · 4 comments
Labels

Comments

@hhstore
Copy link
Owner

hhstore commented Sep 23, 2018

go 包管理工具

对比其他包管理工具:

@hhstore
Copy link
Owner Author

hhstore commented Sep 23, 2018

Go 1.11 Modules:

说明:

ref:

使用方法:

1. 初始化 go.mod 文件:

# 初始化:
$ cd $GOPATH/src/<project path>
$ export GO111MODULE=on

# if using Go 1.11, 'master', or the latest 'vgo', use the newer form:  
$ go mod init   

# in go1.11beta2 and earlier, the older form was:
$ go mod -init    

# 如果go mod无法自动确定适当的模块路径(例如,如果在VCS外部运行),或者您需要覆盖该路径,则可以按如下方式提供模块路径:

# if using Go 1.11, 'master', or the latest 'vgo', use the newer form:  
$ go mod init example.com/my/module/v2

# in go1.11beta2 and earlier, the older form was:
$ go mod -init -module example.com/my/module/v2    



2. 更新项目包依赖:

  • 首次初始化依赖包: go mod tidy

  • https://www.jianshu.com/p/c5733da150c6

  • 会自动更新依赖关系,并且将包下载放入cache

  • 当使用go build,go test以及go list时, go 会自动更新go.mod

# 根据需要的依赖自动生成 require 语句
$ go mod tidy 

3. go get 新特性:

  • go get会自动下载并安装package,然后更新到go.mod中
  • 可以使用go get package[@Version]来安装指定版本的package,不指定version时默认行为和go get package@latest一样
  • version可以是vx.y.z这种形式或者直接使用commit的checksum,也可以是master或者latest
  • 当version是latest时,也就是默认行为,对于有tags的package,会选取最新的tag,对于没有tags的package,则选取最新的commit
$ go get

@hhstore hhstore added the Golang label Sep 23, 2018
@hhstore
Copy link
Owner Author

hhstore commented Mar 13, 2019

go modules 被墙解决办法:

go mod tidy
go: finding google.golang.org/api v0.0.0-20180829000535-087779f1d2c9
go: google.golang.org/[email protected]: git -c protocol.version=0 fetch --unshallow -f https://code.googlesource.com/google-api-go-client refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /Users/henry/iSpace/iGo/pkg/mod/cache/vcs/9e62a95b0409d58bc0130bae299bdffbc7b7e74f3abe1ecf897474cc474b8bc0: exit status 128:
        error: RPC failed; curl 18 transfer closed with outstanding read data remaining
        fatal: The remote end hung up unexpectedly
        fatal: early EOF
        fatal: index-pack failed
go: error loading module requirements

方案1: 修改 go.mod:

  • golang.org 被墙解决办法

  • 修改 `go.mod 配置, 将被墙地址替换到github对应repo:

  • replace:

go mod edit -replace=golang.org/x/build=github.com/golang/build latest
go mod edit -replace=golang.org/x/crypto=github.com/golang/crypto latest
go mod edit -replace=golang.org/x/exp=github.com/golang/exp latest
go mod edit -replace=golang.org/x/image=github.com/golang/image latest
go mod edit -replace=golang.org/x/lint=github.com/golang/lint latest
go mod edit -replace=golang.org/x/mobile=github.com/golang/mobile latest
go mod edit -replace=golang.org/x/net=github.com/golang/net latest
go mod edit -replace=golang.org/x/oauth2=github.com/golang/oauth2 latest
go mod edit -replace=golang.org/x/perf=github.com/golang/perf latest
go mod edit -replace=golang.org/x/review=github.com/golang/review latest
go mod edit -replace=golang.org/x/sync=github.com/golang/sync latest
go mod edit -replace=golang.org/x/sys=github.com/golang/sys latest
go mod edit -replace=golang.org/x/text=github.com/golang/text latest
go mod edit -replace=golang.org/x/tools=github.com/golang/tools latest
go mod edit -replace=golang.org/x/time=github.com/golang/time latest

go mod edit -replace=google.golang.org/api=github.com/googleapis/google-api-go-client latest
go mod edit -replace=google.golang.org/appengine=github.com/golang/appengine latest
go mod edit -replace=google.golang.org/genproto=github.com/google/go-genproto latest
go mod edit -replace=google.golang.org/grpc=github.com/grpc/grpc-go latest

go mod edit -replace=cloud.google.com/go=github.com/googleapis/google-cloud-go latest
  • 修改:
module tx

require github.com/sirupsen/logrus v1.2.0 // indirect

replace (
    golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac => github.com/golang/crypto v0.0.0-20180820150726-614d502a4dac
    golang.org/x/net v0.0.0-20180821023952-922f4815f713 => github.com/golang/net v0.0.0-20180826012351-8a410e7b638d
    golang.org/x/text v0.3.0 => github.com/golang/text v0.3.0
    golang.org/x/sys v0.3.0=>github.com/golang/sys v0.3.0
)




镜像仓库列表:

Google github
google.golang.org/api https://github.com/googleapis/google-api-go-client
google.golang.org/grpc https://github.com/grpc/grpc-go
xxx https://github.com/

方案2: 走proxy:

方案3: 手动拉取github对应repo替换:

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net

# 手动安装所有依赖, 替换掉,再重新安装:
go install google.golang.org/grpc


@hhstore
Copy link
Owner Author

hhstore commented Mar 13, 2019

go mod + go get:

安装指定版本:

$ go get github.com/wilk/[email protected]
$ go get github.com/wilk/[email protected]

go get -u github.com/micro/micro


安装指定分支:

go get github.com/someone/some_module@master
go get github.com/someone/some_module@dev_branch


@hhstore hhstore mentioned this issue Mar 18, 2019
3 tasks
@hhstore
Copy link
Owner Author

hhstore commented Nov 11, 2019

google.golang.org/genproto

golang, google 包列表:

export GO111MODULE=off; go get -u -v google.golang.org/genproto
export GO111MODULE=off; go get -u -v google.golang.org/api
export GO111MODULE=off; go get -u -v google.golang.org/appengine
export GO111MODULE=off; go get -u -v google.golang.org/grpc


protobuf:


# 安装 protobuf:
brew install protobuf


# 安装 swagger:
brew tap go-swagger/go-swagger
brew install go-swagger


# 安装依赖: 
export GO111MODULE=off; go get -u -v github.com/gogo/protobuf/protoc-gen-gofast
export GO111MODULE=off; go get -u -v github.com/gogo/protobuf/proto
export GO111MODULE=off; go get -u -v github.com/gogo/protobuf/{binary}
export GO111MODULE=off; go get -u -v github.com/gogo/protobuf/gogoproto
export GO111MODULE=off; go get -u -v github.com/gogo/protobuf/jsonpb
export GO111MODULE=off; go get -u -v github.com/gogo/protobuf/protoc-gen-gogo



# 在 go.mod 文件路径下执行: 
export GO111MODULE=on; go get -u -v github.com/gogo/protobuf/protoc-gen-gofast
export GO111MODULE=on; go get -u -v github.com/gogo/protobuf/proto
export GO111MODULE=on; go get -u -v github.com/gogo/protobuf/{binary}
export GO111MODULE=on; go get -u -v github.com/gogo/protobuf/gogoproto
export GO111MODULE=on; go get -u -v github.com/gogo/protobuf/jsonpb
export GO111MODULE=on; go get -u -v github.com/gogo/protobuf/protoc-gen-gogo



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant