Preface

prerequisites

Go Modules

An Example

path/to/my-repo:
bar:
go.mod
bar-file1.go
bar-file2.go
foo:
foo-file1.go
foo-file2.go
mixi:
go.mod
mixi-file1.go
mixi-file2.go
module path/to/my-repo/bar

require (
golang.org/x/text v0.3.0
rsc.io/sampler v1.99.99
// Other dependencies
)
import "path/to/my-repo/bar/foo"

func main () {
foo.DoSomething()
}

How to Enable Go Modules

When to Use Go Modules

Semantic Import Versioning

Releasing

git tag bar/v2.3.3 && git push -q origin master bar/v2.3.3

Utilizing Go Modules

General Guide of Converting Go Packages to Go Modules

Converting

module github.com/azhuox/blogs/golang/go_modules/example/module

go 1.12

require (
golang.org/x/net v0.0.0-20190328230028-74de082e2cca
rsc.io/quote v1.5.2
)

Releasing

Consuming A Module

[[constraint]]
name = "github.com/azhuox/blogs"
branch = "master"

Converting Go Libraries to Go Modules

go mod init github.com/azhuox/blogs/golang/go_modules/example/libs/libc
go: creating new go.mod: module github.com/azhuox/blogs/golang/go_modules/example/libs/libc
go build:

can't load package: package github.com/azhuox/blogs/golang/go_modules/example/libs/libc: unknown import path "github.com/azhuox/blogs/golang/go_modules/example/libs/libc": ambiguous import: found github.com/azhuox/blogs/golang/go_modules/example/libs/libc in multiple modules:
github.com/azhuox/blogs/golang/go_modules/example/libs/libc (/Users/achuo/go/src/github.com/azhuox/blogs/golang/go_modules/example/libs/libc)
github.com/azhuox/blogs v0.0.0-20190330175117-09a7dbd4a3ce (/Users/achuo/go/pkg/mod/github.com/azhuox/blogs@v0.0.0-20190330175117-09a7dbd4a3ce/golang/go_modules/example/libs/libc)
cd path/to/libs/liba
go mod init github.com/azhuox/blogs/golang/go_modules/example/libs/liba
go: creating new go.mod: module github.com/azhuox/blogs/golang/go_modules/example/libs/liba
go build
go: finding golang.org/x/net/context latest
go: finding golang.org/x/net latest

# Commit changes
#
git add ./go.mod ./go.sum
git commit ./go.mod ./go.sum -q -m "Convert liba to a module" && git push origin master -q

# Release the latest version (v1.1.0):
#
git tag golang/go_modules/example/libs/liba/v1.1.0 && git push -q origin master golang/go_modules/example/libs/liba/v1.1.0
go mod init github.com/azhuox/blogs/golang/go_modules/example/libs/libb
go: creating new go.mod: module github.com/azhuox/blogs/golang/go_modules/example/libs/libb
go build
go: downloading github.com/azhuox/blogs/golang/go_modules/example/libs/liba v1.1.0
go: extracting github.com/azhuox/blogs/golang/go_modules/example/libs/liba v1.1.0
...

git add ./go.mod ./go.sum
git commit ./go.mod ./go.sum -q -m "Convert libb to a module" && git push origin master -q
git tag golang/go_modules/example/libs/libb/v1.0.0 && git push -q origin master golang/go_modules/example/libs/libb/v1.0.0
go mod init github.com/azhuox/blogs/golang/go_modules/example/libs/libc
go build
go: downloading github.com/azhuox/blogs/golang/go_modules/example/libs/libb v1.0.0
go: extracting github.com/azhuox/blogs/golang/go_modules/example/libs/libb v1.0.0
...

git add ./go.mod ./go.sum
git commit ./go.mod ./go.sum -q -m "Convert libc to a module" && git push origin master -q
git tag golang/go_modules/example/libs/libc/v1.0.0 && git push -q origin master golang/go_modules/example/libs/libc/v1.0.0

Go Modules and Microservices

github.com/azhuox/blogs/tree/master/golang/go_modules/example/micro-service:
- sdks
- go
- internal
- api
- pkga
- pkgb
- server
- main.go
- vendor
- Gopkg.toml
- Gopkg.lock
- Dockerfile
FROM golang:1.12-alpine3.9

RUN apk add --update \
ca-certificates \
git

COPY . $GOPATH/src/github.com/azhuox/blogs/golang/go_modules/example/micro-service
RUN go build -o /usr/bin/micro-service github.com/azhuox/blogs/golang/go_modules/example/micro-service/server && rm -rf $GOPATH/*

ENTRYPOINT ["/usr/bin/micro-service"]
go mod init github.com/azhuox/blogs/golang/go_modules/example/micro-service/sdks/go
go build
...
git add ./go.mod ./go.sum
git commit ./go.mod ./go.sum -q -m "Convert micro-service/sdks/go to a module" && git push origin master -q
git tag golang/go_modules/example/micro-service/sdks/go/v1.0.2 && git push -q origin master golang/go_modules/example/micro-service/sdks/go/v1.0.2

CI Without Vendor

CI With Vendor

Update A Dependency in the vendor Directory

replace (
github.com/azhuox/blogs/golang/go_modules/example/libs/libc v1.0.0 github.com/azhuox/blogs/golang/go_modules/example/libs/libc v1.5.0
)

Summary

Reference

A software engineer