提交 925ad65f 编写于 作者: 写代码的明哥's avatar 写代码的明哥

Add:新增文章(如何发布开源包给别人用)

上级 9a70500a
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
- 3.2 [Go语言命名编码规范](http://golang.iswbm.com/en/latest/c03/c03_02.html) - 3.2 [Go语言命名编码规范](http://golang.iswbm.com/en/latest/c03/c03_02.html)
- 3.3 [go 命令详解](http://golang.iswbm.com/en/latest/c03/c03_03.html) - 3.3 [go 命令详解](http://golang.iswbm.com/en/latest/c03/c03_03.html)
- 3.4 [Go 语言中关于包导入必学的 8 个知识点](http://golang.iswbm.com/en/latest/c03/c03_04.html) - 3.4 [Go 语言中关于包导入必学的 8 个知识点](http://golang.iswbm.com/en/latest/c03/c03_04.html)
- 3.5 [如何开源自己写的包给别人用?](http://golang.iswbm.com/en/latest/c03/c03_05.html)
## 第四章:并发编程 ## 第四章:并发编程
- 4.1 [一篇文章理解 Go 里的函数](http://golang.iswbm.com/en/latest/c04/c04_01.html) - 4.1 [一篇文章理解 Go 里的函数](http://golang.iswbm.com/en/latest/c04/c04_01.html)
...@@ -34,12 +35,11 @@ ...@@ -34,12 +35,11 @@
- 4.4 [几个信道死锁经典错误案例详解](http://golang.iswbm.com/en/latest/c04/c04_04.html) - 4.4 [几个信道死锁经典错误案例详解](http://golang.iswbm.com/en/latest/c04/c04_04.html)
- 4.5 [学习 Go 协程:WaitGroup](http://golang.iswbm.com/en/latest/c04/c04_05.html) - 4.5 [学习 Go 协程:WaitGroup](http://golang.iswbm.com/en/latest/c04/c04_05.html)
- 4.6 [学习 Go 协程:互斥锁和读写锁](http://golang.iswbm.com/en/latest/c04/c04_06.html) - 4.6 [学习 Go 协程:互斥锁和读写锁](http://golang.iswbm.com/en/latest/c04/c04_06.html)
- 4.7 [如何把自己写的包上传到github给别人用](http://golang.iswbm.com/en/latest/c04/c04_07.html)
- 4.8 [学习一些常见的并发模型](http://golang.iswbm.com/en/latest/c04/c04_08.html) - 4.8 [学习一些常见的并发模型](http://golang.iswbm.com/en/latest/c04/c04_08.html)
- 4.9 [函数式编程](http://golang.iswbm.com/en/latest/c04/c04_09.html) - 4.9 [函数式编程](http://golang.iswbm.com/en/latest/c04/c04_09.html)
## 第五章:暂未分类 ## 第五章:暂未分类
- 3.10 [推荐几个Go学习网站](http://golang.iswbm.com/en/latest/c05/c01_01.html) - 5.1 [推荐几个Go学习网站](http://golang.iswbm.com/en/latest/c05/c01_01.html)
--- ---
......
# 3.5 如何开源自己写的包给别人用?
通常之前的学习,我们知道了在 Go 的项目中,可以 import 一个托管在远程仓库的模块,这个模块在我们使用 go get 的时候,会下载到本地。
既然是放在远程仓库上,意味着所有人都可以发布,并且所以人也都可以使用。
今天就来学习一下,如何发布一个开源的模块,并且使用它。
## 1. 新建仓库
先在你的 Github 上新建一个仓库,记得选 Public(默认)
![](http://image.python-online.cn/image-20200317202948177.png)
然后你会得到一个仓库地址,在你的电脑上 使用 `git clone` 命令克隆下来
## 2. 编写模块代码
使用前面学过的 go mod init 命令进行初始化,注意这里的模块名,填写我们的git仓库地址(但是要去掉`.git`哈)
```
$ git clone https://github.com/BingmingWong/goutils.git
$ go mod init github.com/BingmingWong/goutils
```
![](http://image.python-online.cn/image-20200317211914020.png)
然后新建一个 hash 文件夹,存放编写的一个计算 md5 值工具包,编辑 `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))
}
```
由于我们使用的都是内置包,没有引入第三方的包,所以接下来可以把你刚刚那些新增的文件,全部 push 到 git 仓库。
```shell
$ git add -A
$ git commit -m "Add a md5 function"
$ git push
```
## 3. 发布版本
一切完成后,刷新我们的仓库,就可以看到我们的刚刚上传的项目代码了,点击 release 发布一个版本
![](http://image.python-online.cn/image-20200317212645500.png)
![](http://image.python-online.cn/image-20200317212816613.png)
然后像下图一样,添加一些版本说明
![](http://image.python-online.cn/image-20200317213121828.png)
最后点击一个 `Publish release`,就发布了一个版本
![](http://image.python-online.cn/image-20200317213331606.png)
## 4. 如何使用?
使用 go get 命令下载我们的发布的模块
```shell
$ go get github.com/BingmingWong/goutils
```
![](http://image.python-online.cn/image-20200321130405670.png)
再使用 tree 命令,查看一下我们下载的包已经放入了 `$GOPATH/pkg/mod` 下。
有一点很有趣的是,我的 Github 用户名(BingmingWong)是有大写字母的,下载下来后,在目录中`大写字母`会对应变成 `!小写字母`,如下所示
![](http://image.python-online.cn/image-20200321130456438.png)
这个用户名看起来有点非主流,你要想改的话,也是可以的。如果你有其他的开源项目,github 并不会为你做重定向,你需要自己评估这个风险。
![](http://image.python-online.cn/image-20200321132052173.png)
回过头来,我还是继续讲如何使用吧。
下载下来后,我们试着去调用一下他的函数,有一点需要注意的是,在这个示例里,你不能使用 `github.com/BingmingWong/goutils` 去导入,因为在这个目录下并没有 `package`,所以你必须导入 `github.com/BingmingWong/goutils/hash`
整个过程如下所示,供你参考:
![](http://image.python-online.cn/image-20200321133247067.png)
本文参考学习自:https://studygolang.com/articles/22851
---
![](http://image.python-online.cn/image-20200320125724880.png)
3.5 如何开源自己写的包给别人用?
===============================
通常之前的学习,我们知道了在 Go 的项目中,可以 import
一个托管在远程仓库的模块,这个模块在我们使用 go get
的时候,会下载到本地。
既然是放在远程仓库上,意味着所有人都可以发布,并且所以人也都可以使用。
今天就来学习一下,如何发布一个开源的模块,并且使用它。
1. 新建仓库
-----------
先在你的 Github 上新建一个仓库,记得选 Public(默认)
|image0|
然后你会得到一个仓库地址,在你的电脑上 使用 ``git clone`` 命令克隆下来
2. 编写模块代码
---------------
使用前面学过的 go mod init
命令进行初始化,注意这里的模块名,填写我们的git仓库地址(但是要去掉\ ``.git``\ 哈)
::
$ git clone https://github.com/BingmingWong/goutils.git
$ go mod init github.com/BingmingWong/goutils
|image1|
然后新建一个 hash 文件夹,存放编写的一个计算 md5 值工具包,编辑
``md5.go``
.. code:: 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))
}
由于我们使用的都是内置包,没有引入第三方的包,所以接下来可以把你刚刚那些新增的文件,全部
push git 仓库。
.. code:: shell
$ git add -A
$ git commit -m "Add a md5 function"
$ git push
3. 发布版本
-----------
一切完成后,刷新我们的仓库,就可以看到我们的刚刚上传的项目代码了,点击
release 发布一个版本
|image2|
|image3|
然后像下图一样,添加一些版本说明
|image4|
最后点击一个 ``Publish release``\ ,就发布了一个版本
|image5|
4. 如何使用?
-------------
使用 go get 命令下载我们的发布的模块
.. code:: shell
$ go get github.com/BingmingWong/goutils
|image6|
再使用 tree 命令,查看一下我们下载的包已经放入了 ``$GOPATH/pkg/mod``
下。
有一点很有趣的是,我的 Github
用户名(BingmingWong)是有大写字母的,下载下来后,在目录中\ ``大写字母``\ 会对应变成
``!小写字母``\ ,如下所示
|image7|
这个用户名看起来有点非主流,你要想改的话,也是可以的。如果你有其他的开源项目,github
并不会为你做重定向,你需要自己评估这个风险。
|image8|
回过头来,我还是继续讲如何使用吧。
下载下来后,我们试着去调用一下他的函数,有一点需要注意的是,在这个示例里,你不能使用
``github.com/BingmingWong/goutils`` 去导入,因为在这个目录下并没有
``package``\ ,所以你必须导入 ``github.com/BingmingWong/goutils/hash``
整个过程如下所示,供你参考:
|image9|
本文参考学习自:https://studygolang.com/articles/22851
--------------
|image10|
.. |image0| image:: http://image.python-online.cn/image-20200317202948177.png
.. |image1| image:: http://image.python-online.cn/image-20200317211914020.png
.. |image2| image:: http://image.python-online.cn/image-20200317212645500.png
.. |image3| image:: http://image.python-online.cn/image-20200317212816613.png
.. |image4| image:: http://image.python-online.cn/image-20200317213121828.png
.. |image5| image:: http://image.python-online.cn/image-20200317213331606.png
.. |image6| image:: http://image.python-online.cn/image-20200321130405670.png
.. |image7| image:: http://image.python-online.cn/image-20200321130456438.png
.. |image8| image:: http://image.python-online.cn/image-20200321132052173.png
.. |image9| image:: http://image.python-online.cn/image-20200321133247067.png
.. |image10| image:: http://image.python-online.cn/image-20200320125724880.png
# 4.7 如何把自己写的包上传到github给别人用
如何把自己写的包上传到github给别人用
https://studygolang.com/articles/22826
如何使用 go modules 导入本地包
https://mp.weixin.qq.com/s/jvqjIzfBlGh3vty_qHl50w
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册