美文网首页程序员
如何在Github上发布自己写的代码给别人使用

如何在Github上发布自己写的代码给别人使用

作者: Rollover | 来源:发表于2019-08-16 23:03 被阅读0次

    在日常开发,我们都会用到别人写的第三方库,那么我们也可以把自己写的代码发布到github给别人使用

    首先要新建一个人仓库,命名叫goutil,使用public(私有仓库不能被别人见到哦!)


    publish.png

    然后clone到本地

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

    这里我们使用go mod管理go代码依赖,进入到goutil目录下,执行

    go mod init github.com/startdusk/goutil   # 使用和github.com上的路径做包名
    

    新建文件夹hash,这次我们写个常见的md5加密函数: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))
    }
    

    下面是测试代码: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)
        }
    }
    

    上面的测试代码中的hash是预先计算好的(linux环境下,windows也有类似的命令行工具)

    md5sum ./md5.go
    
    filehash.png
    echo -n "why did you like golang" | md5sum
    
    stringmd5.png

    然后测试,通过就可以上传代码到远程仓库

    runtest.png

    接下来就是git的一些常规操作:

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

    如果需要过滤掉一些不想上传的文件,在.gitignore上添加

    pass.png

    待上传成功后,我们需要发布下代码,进入到git仓库goutil,点击release发布

    release.png

    创建一个新的release,版本号默认为v0.0.0的格式,所以第一次发布我们写v0.1.0,然后添加一些说明


    release2.png

    发布成功后,我们可以来试试自己写的代码能否被使用:

    go get github.com/startdusk/goutil
    
    download.png

    相关文章

      网友评论

        本文标题:如何在Github上发布自己写的代码给别人使用

        本文链接:https://www.haomeiwen.com/subject/cxvcsctx.html