***
#### 在日常开发,我们都会用到别人写的第三方库,那么我们也可以把自己写的代码发布到github给别人使用
首先要新建一个人仓库,命名叫goutil,使用public(私有仓库不能被别人见到哦!)
![publish.png](https://upload-images.jianshu.io/upload_images/19249079-5d49ed3747c93d95.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后clone到本地
```bash
git clone https://github.com/startdusk/goutil.git
```
这里我们使用go mod管理go代码依赖,进入到goutil目录下,执行
```bash
go mod init github.com/startdusk/goutil # 使用和github.com上的路径做包名
```
新建文件夹hash,这次我们写个常见的md5加密函数:goutil/hash/md5.go
```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
```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也有类似的命令行工具)
```bash
md5sum ./md5.go
```
![filehash.png](https://upload-images.jianshu.io/upload_images/19249079-7249576fd8db1ae1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
```bash
echo -n "why did you like golang" | md5sum
```
![stringmd5.png](https://upload-images.jianshu.io/upload_images/19249079-ea11398aabc0f20f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后测试,通过就可以上传代码到远程仓库
![runtest.png](https://upload-images.jianshu.io/upload_images/19249079-5402474661f83d03.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
接下来就是git的一些常规操作:
```bash
git add .
git commit -m "Add a md5 functions"
git push
```
如果需要过滤掉一些不想上传的文件,在.gitignore上添加
![pass.png](https://upload-images.jianshu.io/upload_images/19249079-d63f1b6fb64cd18d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
待上传成功后,我们需要发布下代码,进入到git仓库goutil,点击release发布
![release.png](https://upload-images.jianshu.io/upload_images/19249079-9813c901d7c2b474.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
创建一个新的release,版本号默认为v0.0.0的格式,所以第一次发布我们写v0.1.0,然后添加一些说明
![release2.png](https://upload-images.jianshu.io/upload_images/19249079-8c1dec7974e261f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
发布成功后,我们可以来试试自己写的代码能否被使用:
```bash
go get github.com/startdusk/goutil
```
![download.png](https://upload-images.jianshu.io/upload_images/19249079-e657c4b603e88a02.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
有疑问加站长微信联系(非本文作者))