柠檬星雨 發表於 2021-11-2 08:17:00

Go 日常开发常备第三方库和工具

<p><img src="https://tva1.sinaimg.cn/large/008i3skNly1gvyvycad0ej30rs0rsta4.jpg"></p>
<p>不知不觉写 <code>Go</code> 已经快一年了,上线了大大小小好几个项目;心态也经历了几轮变化。</p>
<p>因为我个人大概前五年时间写的是 <code>Java</code> ,中途写过一年多的 <code>Python</code>,所以刚接触到 Go 时的感觉如下图:<br>
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gvywcqamm1j30ng0l6did.jpg"></p>
<p>既没有 <code>Java</code> 的生态,也没有 <code>Python</code> 这么多语法糖。</p>

<p>写到现在的感觉就是:<br>
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gvywhbgtc3j31360na0wj.jpg"></p>
<p>这里就不讨论这几门语言谁强谁弱了;重点和大家分享下我们日常开发中所使用到的一些第三方库与工具。</p>
<p>这里我主要将这些库分为两类:</p>
<ul>
<li>业务开发</li>
<li>基础工具开发</li>
</ul>
<h1 id="业务开发">业务开发</h1>
<p>首先是业务开发,主要包含了 <code>web</code>、数据库、<code>Redis</code> 等。</p>
<h2 id="gin-️️️️️">Gin ⭐️⭐️⭐️⭐️⭐️</h2>
<p>首先是 Gin,一款 HTTP 框架,使用简单、性能优秀、资料众多;你还在犹豫选择哪款框架时,那就选择它吧,基本没错。</p>
<p>当然和它配套的 github.com/swaggo/gin-swagger swagger 工具也是刚需;利用它可以生成 swagger 文档。</p>
<h2 id="gorm-️️️️️">GORM ⭐️⭐️⭐️⭐️⭐️</h2>
<p>GORM 也没啥好说的,如果你喜欢 <code>orm</code> 的方式操作数据库,那就选它吧;同样的也是使用简单、资料较多。</p>
<p>如果有读写分离需求,也可以使用 <code>GORM</code> 官方提供的插件 https://github.com/go-gorm/dbresolver ,配合 <code>GORM</code> 使用也是非常简单。</p>
<h2 id="errors-️️️️️">errors ⭐️⭐️⭐️⭐️⭐️</h2>
<p>Go 语言自身提供的错误处理比较简单,https://github.com/pkg/errors 提供了更强大的功能,比如:</p>
<ul>
<li>包装异常</li>
<li>包装堆栈等。</li>
</ul>
<p>常用的有以下 API:</p>
<pre><code class="language-go">// WithMessagef annotates err with the format specifier.
func WithMessagef(err error, format string, args ...interface{}) error

// WithStack annotates err with a stack trace at the point WithStack was called.
func WithStack(err error) error
</code></pre>
<h2 id="zorolog-️️️️️">zorolog ⭐️⭐️⭐️⭐️⭐️</h2>
<p>Go 里的日志打印库非常多,日志在日常开发中最好就是存在感低;也就是说性能强(不能影响到业务代码)、使用 API 简单。</p>
<pre><code class="language-go">"github.com/rs/zerolog/log"
log.Debug().Msgf("OrderID :%s", "12121")
</code></pre>
<h2 id="excelize">excelize</h2>
<p>https://github.com/qax-os/excelize是一个读写 Excel 的库,基本上你能遇到的 Excel 操作它都能实现。</p>
<h2 id="now-️️️️">now ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/jinzhu/now 是一个时间工具库:</p>
<ul>
<li>获取当前的年月日、时分秒。</li>
<li>不同时区支持。</li>
<li>最后一周、最后一个月等。</li>
</ul>
<pre><code class="language-go">import "github.com/jinzhu/now"

time.Now() // 2013-11-18 17:51:49.123456789 Mon

now.BeginningOfMinute()      // 2013-11-18 17:51:00 Mon
now.BeginningOfHour()          // 2013-11-18 17:00:00 Mon
now.BeginningOfDay()         // 2013-11-18 00:00:00 Mon
now.BeginningOfWeek()          // 2013-11-17 00:00:00 Sun
now.BeginningOfMonth()         // 2013-11-01 00:00:00 Fri
now.BeginningOfQuarter()       // 2013-10-01 00:00:00 Tue
now.BeginningOfYear()          // 2013-01-01 00:00:00 Tue

now.EndOfMinute()            // 2013-11-18 17:51:59.999999999 Mon
now.EndOfHour()                // 2013-11-18 17:59:59.999999999 Mon
now.EndOfDay()               // 2013-11-18 23:59:59.999999999 Mon
now.EndOfWeek()                // 2013-11-23 23:59:59.999999999 Sat
now.EndOfMonth()               // 2013-11-30 23:59:59.999999999 Sat
now.EndOfQuarter()             // 2013-12-31 23:59:59.999999999 Tue
now.EndOfYear()                // 2013-12-31 23:59:59.999999999 Tue

now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
now.EndOfWeek()                // 2013-11-24 23:59:59.999999999 Sun
</code></pre>
<h2 id="decimal-️️️️">Decimal ⭐️⭐️⭐️⭐️</h2>
<p>当业务上需要精度计算时 https://github.com/shopspring/decimal 可以帮忙。</p>
<pre><code class="language-go">import (
        "fmt"
        "github.com/shopspring/decimal"
)

func main() {
        price, err := decimal.NewFromString("136.02")

        quantity := decimal.NewFromInt(3)
        fee, _ := decimal.NewFromString(".035")
        taxRate, _ := decimal.NewFromString(".08875")

        subtotal := price.Mul(quantity)

        preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))

        total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))

        fmt.Println("Subtotal:", subtotal)                      // Subtotal: 408.06
        fmt.Println("Pre-tax:", preTax)                         // Pre-tax: 422.3421
        fmt.Println("Taxes:", total.Sub(preTax))                // Taxes: 37.482861375
        fmt.Println("Total:", total)                            // Total: 459.824961375
        fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875
}
</code></pre>
<p>基本上你能想到的精度转换它都能做到;配合上 <code>GORM</code> 也可以将 <code>model</code> 字段声明为 <code>decimal</code> 的类型,数据库对应的也是 <code>decimal</code> ,这样使用起来时会更方便。</p>
<pre><code class="language-go">Amount decimal.Decimal `gorm:"column:amout;default:0.0000;NOT NULL" json:"amout"`
</code></pre>
<h2 id="configor-️️️️">configor ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/jinzhu/configor 是一个配置文件读取库,支持 <code>YAML/JSON/TOML</code> 等格式。</p>
<h2 id="go-cache-️️️">go-cache ⭐️⭐️⭐️</h2>
<p>https://github.com/patrickmn/go-cache 是一个类似于 Java 中的 <code>Guava cache</code>,线程安全,使用简单;不需要分布式缓存的简单场景可以考虑。</p>
<pre><code class="language-go">        c := cache.New(5*time.Minute, 10*time.Minute)
        // Set the value of the key "foo" to "bar", with the default expiration time
        c.Set("foo", "bar", cache.DefaultExpiration)
</code></pre>
<h2 id="copier-️️️">copier ⭐️⭐️⭐️</h2>
<p>https://github.com/jinzhu/copier 看名字就知道这是一个数据复制的库,与 <code>Java</code> 中的 <code>BeanUtils.copy()</code> 类似;可以将两个字段相同但对象不同的<code>struct</code> 进行数据复制,也支持深拷贝。</p>
<pre><code class="language-go">func Copy(toValue interface{}, fromValue interface{}) (err error)
</code></pre>
<p>在我们需要一个临时 struct 来存放数据时很有用,特别是一个 struct 中字段非常多时,一个个来回赋值确实有点费手指。</p>
<p>但也要注意不要什么情况都使用,会带来一些弊端:</p>
<ul>
<li>当删除字段时,不能利用编译器提示。</li>
<li>当一些字段需要额外人工处理时,代码不易阅读。</li>
<li>反射赋值,有一定性能损耗。</li>
</ul>
<p>总之在业务开发时,还是建议人工编写,毕竟代码是给人看的。</p>
<h2 id="env-️️️">env ⭐️⭐️⭐️</h2>
<p>https://github.com/caarlos0/env 这个库可以将我们的环境变量转换为一个 <code>struct</code>.</p>
<pre><code class="language-go">type config struct {
        Home string `env:"HOME"`
}

func main() {
        cfg := config{}
        if err := env.Parse(&amp;cfg); err != nil {
                fmt.Printf("%+v\n", err)
        }

        fmt.Printf("%+v\n", cfg)
}
</code></pre>
<p>这个在我们打包代码到不同的运行环境时非常有用,利用它可以方便的获取不同环境变量。</p>
<h2 id="user_agent-️️️">user_agent ⭐️⭐️⭐️</h2>
<p>https://github.com/mssola/user_agent 是一个格式化 <code>user-agent</code> 的小工具。</p>
<p>当我们需要在服务端收集 <code>user-agen</code> 时可以更快的读取数据。</p>
<pre><code class="language-go">func main() {
    ua := user_agent.New("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")

    fmt.Printf("%v\n", ua.Mobile())   // =&gt; true
    fmt.Printf("%v\n", ua.Bot())      // =&gt; false
    fmt.Printf("%v\n", ua.Mozilla())// =&gt; "5.0"
    fmt.Printf("%v\n", ua.Model())    // =&gt; "Nexus One"
    fmt.Printf("%v\n", ua.Platform()) // =&gt; "Linux"
    fmt.Printf("%v\n", ua.OS())
    }
</code></pre>
<h2 id="phonenumbers-️️️">phonenumbers ⭐️⭐️⭐️</h2>
<p>https://github.com/nyaruka/phonenumbers 手机号码验证库,可以不用自己写正则表达式了。</p>
<pre><code class="language-go">// parse our phone number
num, err := phonenumbers.Parse("6502530000", "US")
</code></pre>
<h1 id="基础工具">基础工具</h1>
<p>接下来是一些基础工具库,包含一些主流的存储的客户端、中间件等。</p>
<h2 id="gomonkey-️️️️️">gomonkey ⭐️⭐️⭐️⭐️⭐️</h2>
<p>github.com/agiledragon/gomonkey 是一个 <code>mock</code> 打桩工具,当我们写单元测试时,需要对一些非接口函数进行 <code>mock</code> 会比较困难,这时就需要用到它了。</p>
<p>由于它是修改了调用对应函数时机器跳转指令,而 CPU 架构的不同对应的指令也不同,所以在我们使用时还不兼容苹果的 M1 芯片,不过目前应该已经兼容了,大家可以试试。</p>
<h2 id="goconvey-️️️️️">goconvey ⭐️⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/smartystreets/goconvey 也是配合单元测试的库,可以兼容 <code>go test</code> 命令。</p>
<ul>
<li>提供可视化 web UI。</li>
<li>与 IDE 集成显示单元覆盖率。<br>
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gw01u0t73fj30yk0kkabx.jpg"></li>
</ul>
<h2 id="dig-️️️️️">dig ⭐️⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/uber-go/dig 这是一个依赖注入库,我们这里暂不讨论是否应该使用依赖注入,至少目前我们使用下来还是有几个好处:</p>
<ul>
<li>所有的对象都是单例。</li>
<li>有一个统一的地方管理对象。</li>
<li>使用时直接传递对象当做参数进来即可(容器会自动注入)。</li>
</ul>
<p>当然也有一些不太方便的地方:</p>
<ul>
<li>不熟悉时,一个对象是如何创建的不清楚。</li>
<li>代码不是很好理解。</li>
</ul>
<p>我们内部有自己开发一个业务框架,其中所有的对象都交由 dig 进行管理,使用起来倒也是比较方便。</p>
<h2 id="cobra-️️️️">cobra ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/spf13/cobra是一个功能强大的命令行工具库,我们用它来实现内部的命令行工具,同时也推荐使用 https://github.com/urfave/cli/ 我个人会更习惯用后者,要简洁一些。</p>
<h2 id="bloomrpc-️️️️">BloomRPC ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/uw-labs/bloomrpc 一个 <code>gRPC</code> 可视化工具,比起自己写 <code>gRPC</code> 客户端的代码那确实是要简单许多。</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNly1gw02u882h4j30qo0erwf0.jpg"><br>
但也有些小问题,比如精度。如果是 int64 超过了 2^56 服务端拿到的值会发生错误,这点目前还未解决。</p>
<h2 id="redis-️️️️">redis ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/go-redis/redis/ Redis 客户端,没有太多可说的;发展了许多年,该有的的功能都有了。</p>
<h2 id="elastic-️️️️">elastic ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/olivere/elastic 这也是一个非常成熟的 <code>elasticsearch</code> 库。</p>
<h2 id="resty-️️️️">resty ⭐️⭐️⭐️⭐️</h2>
<p>https://github.com/go-resty/resty/一个 http client, 使用起来非常简单:</p>
<pre><code class="language-go">// Create a Resty Client
client := resty.New()
resp, err := client.R().
    EnableTrace().
    Get("https://httpbin.org/get")
</code></pre>
<p>有点 <code>Python requests</code> 包那味了。</p>
<h2 id="pulsar-client-go-️️️">pulsar-client-go ⭐️⭐️⭐️</h2>
<p>Pulsar 官方出品的 go 语言客户端,相对于 Java 来说其他语言的客户端几乎都是后娘养的;功能会比较少,同时更新也没那么积极;但却没得选。</p>
<h2 id="go-grpc-middleware-️️️">go-grpc-middleware ⭐️⭐️⭐️</h2>
<p>https://github.com/grpc-ecosystem/go-grpc-middleware 官方提供的 <code>gRPC</code> 中间件,可以自己实现内部的一些鉴权、元数据、日志等功能。</p>
<h2 id="go-pilosa-️️️">go-pilosa ⭐️⭐️⭐️</h2>
<p>https://github.com/pilosa/go-pilosa 是一个位图数据库的客户端,位图数据库的场景应用比较有限,通常是有标签需求时才会用到;比如求 N 个标签的交并补集;数据有一定规模后运营一定会提相关需求;可以备着以备不时之需。</p>
<h2 id="pb-️️️">pb ⭐️⭐️⭐️</h2>
<p>https://github.com/cheggaaa/pb 一个命令行工具进度条,编写命令行工具时使用它交互会更优雅。</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNly1gw04urcj16g30gn0571kz.gif"></p>
<h1 id="总结">总结</h1>
<p>最后我汇总了一个表格,方便查看:</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>类型</th>
<th>功能</th>
<th>星级</th>
</tr>
</thead>
<tbody>
<tr>
<td>Gin</td>
<td>业务开发</td>
<td><code>HTTP</code> 框架</td>
<td>⭐️⭐️⭐️⭐️⭐️</td>
</tr>
<tr>
<td>GORM</td>
<td>业务开发</td>
<td><code>ORM</code> 框架</td>
<td>⭐️⭐️⭐️⭐️⭐️</td>
</tr>
<tr>
<td>errors</td>
<td>业务开发</td>
<td>异常处理库</td>
<td>⭐️⭐️⭐️⭐️⭐️</td>
</tr>
<tr>
<td>zorolog</td>
<td>业务开发</td>
<td>日志库</td>
<td>⭐️⭐️⭐️⭐️⭐️</td>
</tr>
<tr>
<td>excelize</td>
<td>业务开发</td>
<td><code>Excel</code>相关需求</td>
<td>⭐️⭐️⭐️⭐️⭐️</td>
</tr>
<tr>
<td>now</td>
<td>业务开发</td>
<td>时间处理</td>
<td>⭐️⭐️⭐️⭐️️</td>
</tr>
<tr>
<td>Decimal</td>
<td>业务开发</td>
<td>精度处理</td>
<td>⭐️⭐️⭐️⭐️️</td>
</tr>
<tr>
<td>configor</td>
<td>业务开发</td>
<td>配置文件</td>
<td>⭐️⭐️⭐️⭐️️</td>
</tr>
<tr>
<td>go-cache</td>
<td>业务开发</td>
<td>本地缓存</td>
<td>⭐️⭐️⭐️</td>
</tr>
<tr>
<td>copier</td>
<td>业务开发</td>
<td>数据复制</td>
<td>⭐️⭐️⭐️️️</td>
</tr>
<tr>
<td>env</td>
<td>业务开发</td>
<td>环境变量</td>
<td>⭐️⭐️⭐️️️</td>
</tr>
<tr>
<td>user_agent</td>
<td>业务开发</td>
<td>读取 <code>user-agent</code></td>
<td>⭐️⭐️⭐️️️</td>
</tr>
<tr>
<td>phonenumbers</td>
<td>业务开发</td>
<td>手机号码验证</td>
<td>⭐️⭐️⭐️️️</td>
</tr>
<tr>
<td>gomonkey</td>
<td>基础工具</td>
<td><code>mock</code>工具</td>
<td>⭐️⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>goconvey</td>
<td>基础工具</td>
<td>单测覆盖率</td>
<td>⭐️⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>dig</td>
<td>基础工具</td>
<td>依赖注入</td>
<td>⭐️⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>cobra</td>
<td>基础工具</td>
<td>命令行工具</td>
<td>⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>cli</td>
<td>基础工具</td>
<td>命令行工具</td>
<td>⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>BloomRPC</td>
<td>基础工具</td>
<td><code>gRPC</code> 调试客户端</td>
<td>⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>redis</td>
<td>基础工具</td>
<td>Redis 客户端</td>
<td>⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>elastic</td>
<td>基础工具</td>
<td><code>elasticsearch</code> 客户端</td>
<td>⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>resty</td>
<td>基础工具</td>
<td>http 客户端</td>
<td>⭐️⭐️⭐️⭐</td>
</tr>
<tr>
<td>pulsar-client-go</td>
<td>基础工具</td>
<td><code>Pulsar</code> 客户端</td>
<td>⭐️⭐️⭐️</td>
</tr>
<tr>
<td>go-grpc-middleware</td>
<td>基础工具</td>
<td><code>gRPC</code> 中间件</td>
<td>⭐️⭐️⭐</td>
</tr>
<tr>
<td>go-pilosa</td>
<td>基础工具</td>
<td><code>pilosa</code> 客户端</td>
<td>⭐️⭐️⭐️</td>
</tr>
<tr>
<td>pb</td>
<td>基础工具</td>
<td>命令行工具进度条</td>
<td>⭐️⭐️⭐️</td>
</tr>
</tbody>
</table>
<blockquote>
<p>星级评分的规则主要是看实际使用的频次。</p>
</blockquote>
<p>最后夹带一点私货(其实也谈不上)<br>
文中提到了我们内部有基于以上库整合了一个业务开发框架;也基于该框架上线了大大小小10几个项目,改进空间依然不少,目前还是在快速迭代中。</p>
<p>大概的用法,入口 <code>main.go</code>:<br>
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gw04ah9an7j30tq0emdhk.jpg"><br>
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gw04ef29pcj31f40go40k.jpg"><br>
最后截取我在内部的分享就概括了整体的思想<code>--引用自公司一司姓同事</code>。</p>
<p>也许我们内部经过多次迭代,觉得有能力开放出来给社区带来一些帮助时也会尝试开源;现阶段就不嫌丑了。</p>
<p>这些库都是我们日常开发最常用到的,也欢迎大家在评论区留下你们常用的库与工具。</p>


</div>
<div id="MySignature" role="contentinfo">
    <div style="font-size: small">

<p>
      作者:
      crossoverJie
</p>
<p>
      出处:
      https://crossoverjie.top
</p>
<img src="https://i.loli.net/2019/05/19/5ce16dbc99cfa13989.jpg">
<p>
欢迎关注博主公众号与我交流。
</p>
<p>
         本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出,
            如有问题, 可邮件(crossoverJie#gmail.com)咨询。
</p>
</div><br><br>
来源:https://www.cnblogs.com/crossoverJie/p/15497124.html
頁: [1]
查看完整版本: Go 日常开发常备第三方库和工具