1. Introduction

During the coding process of Golang project, we often use official dependent packages through import, and also download third-party packages through go get -u github.com/xxx/xxx command. So how do we make our packaged packages open source so that everyone can use our packages like any other three-party package?

2. Create a warehouse

Create a new repository on Github and select Public (default) :After the warehouse is created successfully, we get the warehouse address and passgit cloneCommand or a tool that comes with the IDE, such as Goland, to pull the project locally.

git clone https://github.com/korb1n/test-project.git
Copy the code

3. Build projects and code

At this time, the project is an empty project, choose to use Go mod to manage the dependency package, initialize the project:

go mod init github.com/korb1n/test-project
Copy the code

Please note that the build project name is: github domain name/username/project name (repository name).

Further code for the test package:

package utils

import (
	"fmt"
	"time"
)

func PrintNow(a)  {
	fmt.Println(time.Now())
}
Copy the code

Project directory structure:

4. Commit and publish code

After we finish writing our code, we can upload Push to a remote Git repository:

git add -A
git commit -m "add a function PrintNow"
git push
Copy the code

Note that if you use IDE integrated Git tools such as Goland, it is easier to operate.ideaThe relevant configuration files have been uploaded, we actually only need the pure project code files, which will be generated for the corresponding IDE when pulling the project code later.After the Push succeeds, refresh the repository on GitHub, confirm that the project code has been uploaded, and clicktagCreate Releases a version with Releases Enter the version number, tag it, and complete the description.

5. Import and use packages

After the successful release of our code package, it can be used as usual to use the third-party package directly go get download and import after use:

go get -u github.com/korb1n/test-project
Copy the code
package main

import "github.com/korb1n/test-project/utils"

func main(a)  {
	utils.PrintNow()
}
Copy the code

👏😄 In addition, IN fact, I am more used to using the domestic Gitee, similar to Github, mainly there is no network problems ~