前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang一些常用库(4)

golang一些常用库(4)

原创
作者头像
dbdocker
发布2024-02-04 16:30:07
1260
发布2024-02-04 16:30:07
举报
文章被收录于专栏:go语言go语言

Protocol Buffers 是一种轻量、高效的数据交换格式,适用于结构化数据的序列化。

代码语言:javascript
复制
goCopy code// 定义.proto文件
syntax = "proto3";

package main;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

// 使用protoc生成Go代码
// protoc --go_out=. example.proto

26. github.com/sirupsen/logrus - 日志库

Logrus 是一个功能强大的结构化日志库。

代码语言:javascript
复制
goCopy codepackage main

import (
	"github.com/sirupsen/logrus"
)

func main() {
	logrus.SetFormatter(&logrus.JSONFormatter{})

	logrus.WithFields(logrus.Fields{
		"animal": "walrus",
	}).Info("A walrus appears")
}

27. github.com/spf13/viper - 配置管理库

Viper 已经在之前的回答中提到过,这里再强调其灵活的配置管理功能。

代码语言:javascript
复制
goCopy codepackage main

import (
	"fmt"
	"github.com/spf13/viper"
)

func main() {
	viper.SetConfigFile("config.toml")
	viper.ReadInConfig()

	fmt.Println("Server IP:", viper.GetString("server.ip"))
	fmt.Println("Server Port:", viper.GetInt("server.port"))
}

28. github.com/stretchr/testify/assert - 测试工具库

Testify 已经在之前的回答中提到过,这里再强调其在单元测试中的断言功能。

代码语言:javascript
复制
goCopy codepackage main

import (
	"testing"
	"github.com/stretchr/testify/assert"
)

func add(x, y int) int {
	return x + y
}

func TestAdd(t *testing.T) {
	result := add(2, 3)
	assert.Equal(t, 5, result, "The result should be 5")
}

29. github.com/gorilla/mux - HTTP路由器

Gorilla Mux 是一个强大的HTTP请求路由器。

代码语言:javascript
复制
goCopy codepackage main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

func main() {
	r := mux.NewRouter()

	r.HandleFunc("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		name := vars["name"]
		fmt.Fprintf(w, "Hello, %s!", name)
	})

	http.Handle("/", r)
	http.ListenAndServe(":8080", nil)
}

这些是一些其他基础常用的Go语言库,包括 Protocol Buffers、日志库、配置管理库、测试工具库和HTTP路由器等方面。根据项目的具体需求,你可以选择适用的库来进行开发。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 26. github.com/sirupsen/logrus - 日志库
  • 27. github.com/spf13/viper - 配置管理库
  • 28. github.com/stretchr/testify/assert - 测试工具库
  • 29. github.com/gorilla/mux - HTTP路由器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档