首页 > 其他分享 >【Golang】关于Golang中一些优秀的类库

【Golang】关于Golang中一些优秀的类库

时间:2022-09-27 19:36:25浏览次数:83  
标签:类库 string err 优秀 cmd Golang Println go fmt

一、CLI 命令(spf13/cobra)

GitHub地址:https://github.com/spf13/cobra

 

Cobra既是一个创建强大的现代CLI应用程序的库,也是一个生成应用程序和命令的程序。可以使用这个库来管理命令应用程序,执行runner应用程序,初始化配置,病启动Reast API。

基于Cobra的应用的目录结构:

├── app
│ ├── main.go
│ ├── cmd
│ └────── root.go
│ └────── helper.go
│ └────── version.go

在 app/main.go 中:

package main

import (
	"app/cmd"
)

func main() {
	cmd.Execute()
}

在 app/cmd/root.go 中:

package cmd

import (
	"errors"
	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "git",
	Short: "Git is a distributed version control system.",
	Long:  `Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.`,
	Run: func(cmd *cobra.Command, args []string) {
		Error(cmd, args, errors.New("unrecognized command"))
	},
}

func Execute() {
	rootCmd.Execute()
}

在 app/cmd/helper.go 中:

package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"os"
	"os/exec"
)

func ExecuteCommand(name string, subname string, args ...string) (string, error) {
	args = append([]string{subname}, args...)

	cmd := exec.Command(name, args...)
	bytes, err := cmd.CombinedOutput()
	return string(bytes), err
}

func Error(cmd *cobra.Command, args []string, err error) {
	fmt.Fprintf(os.Stderr, "execute %s args:%v error:%v\n", cmd.Name(), args, err)
	os.Exit(1)
}

在 app/cmd/version.go 中:

package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"os"
)

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "version subcommand show git version info.",

	Run: func(cmd *cobra.Command, args []string) {
		output, err := ExecuteCommand("git", "version", args...)
		if err != nil {
			Error(cmd, args, err)
		}
		fmt.Fprint(os.Stdout, output)
	},
}

func init() {
	rootCmd.AddCommand(versionCmd)
}

 

开始运行

PS G:\go-demo\test11> go run main.go  --help          
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Usage:
  git [flags]
  git [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  version     version subcommand show git version info.

Flags:
  -h, --help   help for git

Use "git [command] --help" for more information about a command.

二、ORM:Gorm

GitHub地址:https://github.com/go-gorm/gorm/

推荐理由:代码优雅,场景非常丰富,满足我们的SQL场景需求,比如:

  • 全功能ORM
  • 关联(拥有一个、拥有多个、属于、多对多、多态、单表继承)
  • Create,Save,Update,Delete,Find 中钩子方法
  • 支持 Preload、Joins 的预加载
  • 事务,嵌套事务,Save Point,Rollback To to Saved Point
  • Context、预编译模式、DryRun 模式
  • 批量插入,FindInBatches,Find/Create with Map,使用 SQL 表达式、Context Valuer 进行 CRUD
  • SQL 构建器,Upsert,锁,Optimizer/Index/Comment Hint,命名参数,子查询
  • 复合主键,索引,约束
  • 自动迁移
  • 自定义 Logger
  • 灵活的可扩展插件 API:Database Resolver(多数据库,读写分离)、Prometheus…
  • 每个特性都经过了测试的重重考验

三、SQL(jmoiron/sqlx)

GitHub地址:https://github.com/jmoiron/sqlx

是sqlx的一个库,在Go的标准database/sql库上提供了一组扩展。

示例:

  place := Place{}
    rows, err := db.Queryx("SELECT * FROM place")
    for rows.Next() {
        err := rows.StructScan(&place)
        if err != nil {
            log.Fatalln(err)
        } 
        fmt.Printf("%#v\n", place)
    }

四、配置管理:viper

GitHub地址:https://github.com/spf13/viper

  • 支持JSON/TOML/YAML/HC:/en:L/envfile/Java properties等多种格式的配置文件;
  • 可以设置监听配置文件的修改,修改时自动加载新的配置;
  • 从环境变量、命令行选项和io。Reader中读取配置;
  • 从远程配置系统中读取和监听修改,如etcd/Consu;
  • 代码逻辑中显示设置键值

五、日志管理:zap

GitHub地址::GitHub - uber-go/zap: Blazing fast, structured, leveled logging in Go.

logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
sugar.Infow("failed to fetch URL",
  // Structured context as loosely typed key-value pairs.
  "url", url,
  "attempt", 3,
  "backoff", time.Second,
)
sugar.Infof("Failed to fetch URL: %s", url)

六、消息队列 asynq

Github地址: GitHub - hibiken/asynq: Simple, reliable, and efficient distributed task queue in Go

可靠、简单、高效的分布式任务队列。

七、消息传递 NSQ

GitHub地址:https://github.com/nsqio/go-nsq

NSQ 拓扑

 


NSQ 组件:

  • nsqlookupd (守护进程管理拓扑 / 路由)
  • nsqd(守护进程管理接收、排队和传递消息)
  • nsqadmin(nsq 的默认 Web UI)

docker-compose 示例:(nsqlookupd, nsqd, nsqadmin)

version: '3'
services:
nsqlookupd:
image: nsqio/nsq
command: /nsqlookupd
ports:
- "4160:4160"
- "4161:4161"
nsqd:
image: nsqio/nsq
command: /nsqd --lookupd-tcp-address=nsqlookupd:4160
depends_on:
- nsqlookupd
ports:
- "4150:4150"
- "4151:4151"
nsqadmin:
image: nsqio/nsq
command: /nsqadmin --lookupd-http-address=nsqlookupd:4161
depends_on:
- nsqlookupd
ports:
- "4171:4171"

执行:

运行 docker:
$ docker-compose up -d
或者,如果使用名称 (docker-compose-nsq.yml):
$ docker-compose -f docker-compose-nsq.yml up -d
检查容器 docker:
$ docker-compose ps
查看日志:
$ docker-compose logs
检查 nsq Web UI(假设端口为 32770):
$ curl http://127.0.0.1:32770/ping

目录结构:

├── consume
│   └── consume.go
└── publish
    └── publish.go

consume.go:

package main
import (
	"github.com/nsqio/go-nsq"
	"log"
	"sync"
)

func main() {
	wg := &sync.WaitGroup{}
	wg.Add(1)
	decodeConfig := nsq.NewConfig()
	c, err := nsq.NewConsumer("My_NSQ_Topic", "My_NSQ_Channel", decodeConfig)
	if err != nil {
		log.Panic("Could not create consumer")
	}
	c.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
		log.Println("NSQ message received:")
		log.Println(string(message.Body))
		return nil
	}))
	err = c.ConnectToNSQD("127.0.0.1:4150")
	if err != nil {
		log.Panic("Could not connect")
	}
	log.Println("Awaiting messages from NSQ topic \"My NSQ Topic\"...")
	wg.Wait()
}

运行 consume.go:

$ go run consume/consume.go

publish.go:

package main
import (
	"log"
	"github.com/nsqio/go-nsq"
)
func main() {
	config := nsq.NewConfig()
	p, err := nsq.NewProducer("127.0.0.1:4150", config)
	if err != nil {
		log.Panic(err)
	}
	err = p.Publish("My_NSQ_Topic", []byte("sample NSQ message"))
	if err != nil {
		log.Panic(err)
	}
}

运行 publish.go:

$ go run publish/publish.go

八、JSON校验库

  • golang json 校验库

GitHub地址:https://github.com/go-playground/validator

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

// User contains user information
type User struct {
	FirstName      string     `validate:"required"`
	LastName       string     `validate:"required"`
	Age            uint8      `validate:"gte=0,lte=130"`
	Email          string     `validate:"required,email"`
	FavouriteColor string     `validate:"iscolor"`                // alias for 'hexcolor|rgb|rgba|hsl|hsla'
	Addresses      []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
}

// Address houses a users address information
type Address struct {
	Street string `validate:"required"`
	City   string `validate:"required"`
	Planet string `validate:"required"`
	Phone  string `validate:"required"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

func main() {

	validate = validator.New()

	validateStruct()
	validateVariable()
}

func validateStruct() {

	address := &Address{
		Street: "Eavesdown Docks",
		Planet: "Persphone",
		Phone:  "none",
	}

	user := &User{
		FirstName:      "Badger",
		LastName:       "Smith",
		Age:            135,
		Email:          "[email protected]",
		FavouriteColor: "#000-",
		Addresses:      []*Address{address},
	}

	// returns nil or ValidationErrors ( []FieldError )
	err := validate.Struct(user)
	if err != nil {

		// this check is only needed when your code could produce
		// an invalid value for validation such as interface with nil
		// value most including myself do not usually have code like this.
		if _, ok := err.(*validator.InvalidValidationError); ok {
			fmt.Println(err)
			return
		}

		for _, err := range err.(validator.ValidationErrors) {

			fmt.Println(err.Namespace())
			fmt.Println(err.Field())
			fmt.Println(err.StructNamespace())
			fmt.Println(err.StructField())
			fmt.Println(err.Tag())
			fmt.Println(err.ActualTag())
			fmt.Println(err.Kind())
			fmt.Println(err.Type())
			fmt.Println(err.Value())
			fmt.Println(err.Param())
			fmt.Println()
		}

		// from here you can create your own error messages in whatever language you wish
		return
	}

	// save user to database
}

func validateVariable() {

	myEmail := "joeybloggs.gmail.com"

	errs := validate.Var(myEmail, "required,email")

	if errs != nil {
		fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
		return
	}

	// email ok, move on
}

九、map to structure

  • golang map to structure 的库

GitHub地址:https://github.com/mitchellh/mapstructure

package main

import (
	"encoding/json"
	"fmt"
	"github.com/mitchellh/mapstructure"
)

type Demo struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func main() {
	var value = make(map[string]Demo)
	value["1"] = Demo{
		ID:   1,
		Name: "name-1",
	}
	valueData, err := json.Marshal(value)
	if err != nil {
		panic(err)
	}
	var newValue interface{}
	if err = json.Unmarshal(valueData, &newValue); err != nil {
		panic(err)
	}
	// 反序列化之后只能强转为map[string]interface{}
	for _, v := range newValue.(map[string]interface{}) {
		/*
			此时可以使用mapstructure.Decode方法编码,该方法使用的是反射的机制;
			也可以对v进行序列化,然后反序列化给demo变量;
		 */
		var demo Demo
		if err = mapstructure.Decode(v, &demo); err != nil {
			panic(err)
		}
		fmt.Println(demo)
	}
}

  

标签:类库,string,err,优秀,cmd,Golang,Println,go,fmt
From: https://www.cnblogs.com/chenpingzhao/p/16729486.html

相关文章

  • Golang Plan9汇编研究——BP,SP和(SP)
    最近有时间研究下Golang的汇编,在开始之前,最好能了解下X86的汇编。Go汇编使用的是Plan9汇编,网上有很多资料可以查看,不过这篇我打算从自己亲手调试过的代码开始记录。Go使用......
  • 密码学奇妙之旅、02 混合加密系统、AES、RSA标准、Golang代码
    CTR计数器模式计数器模式CTR是分组密码模式中的一种。通过将逐次累加的计数器进行加密来生成密钥流的流密码。每次加密时会生成一个不同的值来作为计数器的初始值。可......
  • golang程序健壮性相关细节
    结构体定义1、请问下面输出是什么packagemainimport("fmt")funcmain(){l:=get()fmt.Printf("%p",l)fmt.Printf("%p",l.List)for......
  • golang 中 取切片元素 与 切片再生成切片的区别
    funcmain(){//定义一个切片a:=[]byte{1,2,3}fmt.Printf("a的类型%T\n",a)//a的类型[]uint8fmt.Printf("a原来的值%v\n",a)//a原来的值[1......
  • golang面试题3
    go基础1、redis部署多节点模式,异步队列2、go-redis和redis-go//go-redis的连接模式,直连哨兵3、go异常处理,异常捕获方式,go里面替代try-catch如何操作4、gomaxprocs的默认......
  • 密码学奇妙之旅、01 CFB密文反馈模式、AES标准、Golang代码
    CFB密文反馈模式CFB密文反馈模式属于分组密码模式中的一种。加密与解密使用同一结构,加密步骤生成用于异或的密钥流。其弥补了ECB电子密码本模式的不足(明文中的重复排列会......
  • golang中下划线的作用
    1.忽略返回值2判断一个类是否实现了某个接口//定义了一个animal接口1typeAnimalinterface{Say()}//定义了一个dog结构体typeDogstruct{}//用来判断Do......
  • 【golang】json数据解析 - 嵌套json解析
    @目录1.通过结构体映射解析2.嵌套json解析-map1.通过结构体映射解析原数据结构解析//结构体typecontractJsonstruct{ Data[]transaction`json:"data"` T......
  • golang goroutine协程泄露
      语言级别的并发支持是Go的一大优势,但这个优势也很容易被滥用。通常我们在开始Go并发学习时,常常听别人说,Go的并发非常简单,在调用函数前加上go关键词便可启动g......
  • golang值传递和引用传递
    Go中所有的参数传递都是值传递,拷贝的都是一个副本。但是,这里分两种情况讨论,引用类型 和 非引用类型。非引用类型(值类型):int,string,float,bool,数组和struct;特点:值类型变量......