In Golang development, we often use open source Golang frameworks or libraries from other organizations or individuals, such as github.com/spf13/viper, which can be downloaded to the local $GOPATH using the command:

go get github.com/spf13/viper
Copy the code

So how to use Github to host their own tool libraries, so that others can easily use? This is a simple demonstration.

Create a github repository to host the go library code, such as common-go:

2. Clone warehouse to local:

git clone https://github.com/vsixz/common-go.git
Copy the code

Initialize the go library module:

cd common-go
go mod init github.com/vsixz/common-go
Copy the code

Note:

Run the go env command to check whether the go-module function is enabled. If not, set the environment variable: go env -w GO111MODULE=on;

The module name needs to be consistent with the Github repository so that others can download to your library via go Get github.com/vsixz/commmon-go.

4. Write the go library code, for example:

5. Submit the go code to Github:

git add .
git commit -m "add hello"
git push -u origin main
Copy the code

6. Release

The best practice is to create the corresponding release branch, and then use the release branch to create the tag and publish:

Git checkout -b release/v1.x git tag v1.0.0 git tagCopy the code

At this point, you can see the released version in the Github repository release

7, Create demo- Go project, test using go library:

go mod init demo-go
Copy the code

Introduce github.com/vsixz/[email protected] in go.mod:

To call the hello.Say method of github.com/vsixz/common-go library:

8. Operation:

$ go run main.go
test hello:
Hello, Jay
Copy the code