Home  >  Article  >  Backend Development  >  How to open source your own Go library

How to open source your own Go library

Go语言进阶学习
Go语言进阶学习forward
2023-07-24 15:15:42813browse
#When we develop Go projects, we often use some external dependency packages. They are generally obtained for local use through commands in the form <span style="font-size: 15px;">go get example.com/xxx</span>.

In the spirit of open source, if we want to share the packages we develop so that others can use them<span style="font-size: 15px;">go What should I do if the get</span> command obtains them?

This article will take open source to the Github platform as an example to show the process.

Create a warehouse

First, create a warehouse on the github platform and set the name of the Go library you want to open source. Here, the project name is publishdemo and the status is Public.

How to open source your own Go library

Develop module code

Pull the created warehouse to the local through the git clone command development.

$ git clone git@github.com:golangShare/publishdemo.git

At this time, under the project root directory<span style="font-size: 15px;">publishdemo/</span>, only the LICENSE and README files are included, and no Go code is included.

$ ls
LICENSE   README.md

Initialize the mod file for the project

$ go mod init github.com/golangShare/publishdemo

Assume that we want to open source the Go tool class library. At this time, we are going to provide it first String-related operations. Therefore, in the <span style="font-size: 15px;">publishdemo/</span> directory, we add the <span style="font-size: 15px;">stringutil/</span> subdirectory , add the reverse.go file in the subdirectory, its content is as follows.

package stringutil

// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
 r := []rune(s)
 for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
  r[i], r[j] = r[j], r[i]
 }
 return string(r)
}

It’s very simple, what we want to provide is a flip string function.

Of course, we should also provide test code. Add the reverse_test.go file in the same level directory with the following content.

package stringutil

import "testing"

func TestReverse(t *testing.T) {
 for _, c := range []struct {
  in, want string
 }{
  {"Hello, world", "dlrow ,olleH"},
  {"Hello, 世界", "界世 ,olleH"},
  {"", ""},
 } {
  got := Reverse(c.in)
  if got != c.want {
   t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
  }
 }
}

Return to the project root directory. At this time, the code structure is as follows

.
├── LICENSE
├── README.md
├── go.mod
└── stringutil
    ├── reverse.go
    └── reverse_test.go

1 directory, 5 files

The test codes also passed

$ go test ./...
ok   github.com/golangShare/publishdemo/stringutil 0.005s

After the development is completed, we can share the tool library.

Release

#To avoid recording dependencies that are no longer needed in the module, execute go mod tidy to remove them.

$ go mod tidy

Use the git tag command to mark version information

$ git commit -m "add Reverse: for v0.1.0"
$ git tag v0.1.0

Push it to the remote warehouse

$ git push origin v0.1.0

使用

发布之后,其他项目就可以通过以下命令获取我们开源的 Go 包了。

$ go get github.com/golangShare/publishdemo@v0.1.0

此时项目 go.mod 文件中,将会增加以下一行记录

require github.com/golangShare/publishdemo v0.1.0

和其他三方库一样的方式使用即可

package main

import (
 "fmt"
 "github.com/golangShare/publishdemo/stringutil"
)

func main() {
 s := stringutil.Reverse("hello gopher")
 fmt.Println(s)
}

总结

看完了上述流程,可以发现:开源自己的 Go 库,其实非常简单。

The above is the detailed content of How to open source your own Go library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete