How to publish your own code on Github for others to use

Keywords: Go git github encoding Windows

In daily development, we all use third-party libraries written by others, so we can also publish our code to github for others to use.

First, we need to build a new warehouse named goutil and use public (private warehouse can't be seen by others!).

Then clone goes to the local place.

git clone https://github.com/startdusk/goutil.git

Here we use go mod to manage go code dependencies, go into the goutil directory, and execute

go mod init github.com/startdusk/goutil   # Use paths on github.com for package names

New folder hash, this time we write a common MD5 encryption function: goutil/hash/md5.go

package hash
​
import (
    "crypto/md5"
    "encoding/hex"
    "errors"
    "fmt"
    "io"
    "os"
)
​
// get file md5
func FileMd5(filename string) (string, error) {
    file, err := os.Open(filename)
    if err != nil {
    return "", errors.New(
        fmt.Sprintf("md5.go hash.FileMd5 os open error %v", err))
    }
    h := md5.New()
    _, err = io.Copy(h, file)
    if err != nil {
        return "", errors.New(fmt.Sprintf("md5.go hash.FileMd5 io copy error %v", err))
    }
​
    return hex.EncodeToString(h.Sum(nil)), nil
}
​
// get string md5
func StringMd5(s string) string {
    md5 := md5.New()
    md5.Write([]byte(s))
    return hex.EncodeToString(md5.Sum(nil))
}

Here is the test code: goutil/hash/md5_test.go

package hash
​
import "testing"
​
func TestMd5(t *testing.T) {
    const expectFileMd5 = "7861252f1d3f3bc4e377782deb212dca"
    actualFileMd5, err := FileMd5("./md5.go")
    if err != nil {
        panic(err)
    }
​
    if expectFileMd5 != actualFileMd5 {
        t.Errorf("expect file md5 is %s; but had %s\n", expectFileMd5, actualFileMd5)
    }
​
    const str = "why did you like golang"
    const expectStringMd5 = "09a6f16fc1e802003b4c0c11b69761d2"
    actualStringMd5 := StringMd5(str)
    if expectStringMd5 != actualStringMd5 {
        t.Errorf("expect string md5 value is %s; but had %s\n", expectStringMd5, actualStringMd5)
    }
}
​
func BenchmarkMd5(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _, err := FileMd5("./md5.go")
        if err != nil {
            panic(err)
        }
    }
​
    const str = "why did you like golang"
    for i := 0; i < b.N; i++ {
        _ = StringMd5(str)
    }
}

The hash in the test code above is pre-computed (windows has similar command-line tools in linux environments)

md5sum ./md5.go

echo -n "why did you like golang" | md5sum

Then test and upload the code to the remote warehouse.

Next comes some of the git's regular operations:

git add .
git commit -m "Add a md5 functions"
git push

If you need to filter out some files that you don't want to upload, add them to. gitignore

After the upload is successful, we need to publish the following code, go to the git repository goutil, and click release to publish.


Create a new release, the version number defaults to v0.0.0 format, so for the first release we write v0.1.0, and then add some instructions

After a successful release, we can try to see if our own code can be used:

go get github.com/startdusk/goutil

Posted by lisa71283 on Thu, 03 Oct 2019 07:05:40 -0700