When we create an aswsome project written by Go, we always want to provide it for others.
Some good projects like gorilla websocket, gorm and gin, we can easily to run go get github.com/xxx to get the Go module and then use it in our projects.
So, how can we build our own Go project as a Go module?

Github

In the platform of Github, you may want to build your projects for all the users so they can use your project directly.
The first step for you is create a repo(here, I create a repo named add):
And then, pull this project to your PC by git(if you feel some difficult using Git, you best follow the official guide).
Now, you can write your codes here, the architechture of your project may best follow this:

test
    - test.go
src
    - source.go
main.go
go.sum
go.mod

Remenber, src/source.go just an example, you can modify it if you like.
Your main functions should be existed in main.go(you still can change the name if you like.) so that users can call your functions.
Here’s my example of function Add(just an example, pls igore too simple of it.)

add.go
go.mod

add.go

1
2
3
4
5
package goModule

func Add(a, b int) int {
    return a + b
}

When you finish your codes, you can pull it to gihub.
We are already finishing our work, and the next step is create a tag and a release version.
Note: In github, you should create tag first then you can create a release version.
After you create the release version, then you can go get github.com/wuqiangroy/add to get the latest version of your project, and you can go get github.com/wuqiangroy/add@{version_id} to get the specific version.
Now, your work’s done.
You can try you Go module in your new project.
You can use it as other open-source projects:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import (
    "fmt"
    "github.com/wuqiangroy/add"
)

func main() {
    var a, b = 1, 2
    var c = add.Add(a, b)
    fmt.Println(c)
}

Company

In company, there are so much different, due to the security and comercial consideration, the company DO NOT want others get the codes, expecially the competitors.
After all, a stuff still not easy to get the code from code lib of the company by web page.The company may enable two-factories verification, and the web protocol of go get is HTTP, we want to use git to replace it HTTP.
The most companies around the world may use Gitlab to build it as own git service, so as your company.
The first step is to generate the access token.
And the official has a very full guide of how to generate an revoke the access token, you can follow it.
Note:Remenber your access token when it display, it only display once and disappear when you refresh the webpage.

And next edit your local git file vim ~/.gitconfig:

[http]
    extraheader = PRIVATE-TOKEN:{your access token}
    sslVerify = false
[url `ssh://git@{the git url of your company}`]
    insteadOf = `https://{the git url of your company}`

save it and try to go get the module in your company lib.

NOTE:
For both above, you should enable the go moduel:
go env -w GO111MODULE=on
If you use go proxy, you may add it to env:
go env -w GOPRIVATE={the git url of your company}