20 Golang中使用第三方包


本文摘自网络,作者,侵删。

在https://pkg.go.dev/查找常见的golang第三方包

1. 找到需要下载安装的第三方包的地址
  • 以解决float精度丢失的包decimal为例

https://github.com/shopspring/decimal

2. 安装该包
  • 方法一
go get 包名称(全局)
//eg:go get github.com/shopspring/decimal
  • 方法二

依赖包会自动下载到$GOPATH/pkg/mod,多个项目可以共享缓存的mod,注意使用go mod download的时候首先需要在你的项目里面引入第三方包

go mod download
  • demo
package main
import (
    "fmt"
    "github.com/shopspring/decimal"
)

func main(){
    var num1 float64 = 3.1
    var num2 float64 = 4.2
    d1 := decimal.NewFromFloat(num1).Add(decimal.NewFromFloat(num2))
    fmt.Println(d1)//7.3
}
  • 方法三

将依赖复制到当前项目的vender下,使用时需要在项目里引入第三方包

go mod vendor
  • get mod命令

命令 说明
init 在当前文件夹下初始化一个新的module,创建go.mod文件
download 下载依赖的module到本地cache
edit 编辑go.mod文件
tidy 增加丢失的module,去掉未用的module
vendor 将依赖复制到vendor下

  • gjson包
package main

import "github.com/tidwall/gjson"

const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`

func main() {
    value := gjson.Get(json, "name.last")
    println(value.String())
}

本文来自:简书

感谢作者:learninginto

查看原文:20 Golang中使用第三方包

相关阅读 >>

如何编写Go中间件

Go入门-1 变量

Golang 是面向对象的么

求助!为何报错?

Go之旅(五)- 基本类型

Golang基础-内置数据结构

Golang 免费的吗?

[系列] Go - 学习 grpc.dial(target string, opts …dialoption) 的写法

[系列] - Go-gin-api 路由中间件 - 日志记录(三)

Golang 包怎么调用

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...