定时器context select go
Go每日一库之107:errors
> 目前实现最优雅,打印堆栈良好的错误包。 # 下载安装 `go get -x github.com/pkg/errors` # 主要函数 ```go // 新生成一个错误, 带堆栈信息 func New(message string) error //只附加新的信息 func WithMessag ......
Go每日一库之106:hystrix(熔断)
## 背景 随着微服务的流行,服务之间的调用可能变得越来越复杂,一个业务流程可能需要调用五六个甚至更多服务,这就会导致,假设某个服务出现问题,严重可能出现服务器负载过高,导致服务雪崩的现象。 因此为了防止此现象的发生,就需要考虑服务熔断机制,根据自身业务的需求,将其应用到服务中。 `什么是熔断`:可 ......
Go每日一库之105:juju%2Fratelimit
限流又称为流量控制(流控),通常是指限制到达系统的并发请求数,常用的限流算法主要有漏洞和令牌桶。 ### 令牌桶 令牌桶其实和漏桶的原理类似,令牌桶按固定的速率往桶里放入令牌,并且只要能从桶里取出令牌就能通过,令牌桶支持突发流量的快速处理。 ![每日一库之105:juju%2Fratelimit-0 ......
Go每日一库之69:resty
## 简介 [`resty`](https://github.com/go-resty/resty)是 Go 语言的一个 HTTP client 库。`resty`功能强大,特性丰富。它支持几乎所有的 HTTP 方法(GET/POST/PUT/DELETE/OPTION/HEAD/PATCH等),并 ......
Go每日一库之70:termtables
## 简介 今天学个简单点的,[`termtables`](github.com/scylladb/termtables)处理表格形式数据的输出。适用于随时随地的输出一些状态或统计数据,便于观察和调试。是一个很小巧的工具库。我在学习[`dateparse`](https://go-quiz.gith ......
Go每日一库之85:freecache
# [freecache](https://github.com/coocood/freecache) ## 一句话描述 Go缓存库,具有零GC开销和高并发性能 ## 简介 ### freecache是什么? 使用FreeCache,您可以在内存中缓存无限数量的对象,而不会增加延迟和降低吞吐量。 # ......
Go每日一库之86:zerolog
## [zerolog](https://github.com/rs/zerolog) ## 什么是 Zerolog ? zerolog 包提供了一个专门用于 JSON 输出的简单快速的Logger。 zerolog 的 API 旨在为开发者提供出色的体验和令人惊叹的[性能](https://git ......
Go每日一库之87:zap
## [⚡ZAP](https://github.com/uber-go/zap) ## 简介 ### zap 是什么? [⚡ZAP](https://github.com/uber-go/zap) 是uber 开源的提供快速,结构化,高性能的日志记录包。 ### zap 高性能体现在哪里? 在介绍 ......
Go每日一库之73:gorilla-mux
## 简介 [`gorilla/mux`](https://github.com/gorilla/mux)是 gorilla Web 开发工具包中的路由管理库。gorilla Web 开发包是 Go 语言中辅助开发 Web 服务器的工具包。它包括 Web 服务器开发的各个方面,有表单数据处理包[`g ......
Go每日一库之74:gorilla-handlers
## 简介 上一篇文章中,我们介绍了 gorilla web 开发工具包中的路由管理库[`gorilla/mux`](https://github.com/gorilla/mux),在文章最后我们介绍了如何使用中间件处理通用的逻辑。在日常 Go Web 开发中,开发者遇到了很多相同的中间件需求,[g ......
Go每日一库之75:gorilla-schema
## 简介 [`gorilla/schema`](https://github.com/gorilla/schema) 是 gorilla 开发工具包中用于处理表单的库。它提供了一个简单的方式,可以很方便地将表单数据转为结构体对象,或者将结构体对象转为表单数据。 ## 快速使用 本文代码使用 Go ......
Go每日一库之72:net-http(基础和中间件)
## 简介 几乎所有的编程语言都以`Hello World`作为入门程序的示例,其中有一部分以编写一个 Web 服务器作为实战案例的开始。每种编程语言都有很多用于编写 Web 服务器的库,或以标准库,或通过第三方库的方式提供。Go 语言也不例外。本文及后续的文章就去探索 Go 语言中的各个Web 编 ......
06. 系统滴答定时器
一、SysTick定时器简介 SysTick,即系统滴答定时器,是属于 CM3 内核中的一个外设,内嵌在 NVIC 中。系统定时器是一个 24bit 的向下递减的计数器,SysTick 的时钟源自 HCLK。当计数值减到 0 时,将从 RELOAD 寄存器中自动重装载定时初值,开始新一轮计数。只要不 ......
Go - Using Log Levels
Examples of log levels from high to low are: • Fatal • Error• Warn • Info • Debug To set up log levels for your logs, you can add the level to each li ......
Go - Logging to File
Problem: You want to log events to a logfile instead of standard error. Solution: Use the SetOutput function to set the log to write to a file. You us ......
Go - Change What Is Being Logged by the Standard Logger
Problem: You want to change what the standard logger logs. Solution: Use the SetFlags function to set flags and add fields to each log line. The defau ......
Go - logging
The log package provides several functions that allow you to write logs. In particular, there are three sets of functions:Print • Prints the logs to t ......
Go - Inspecting Errors
Problem: You want to check for specific errors or specific types of errors. Solution: Use the errors.Is and errors.As functions. The errors.Is functio ......
【学到一个小技巧】context.WithValue 中的 key 可以是个空对象
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 从 netpoll 库看到这段代码: type connkey struct{} var ctxkey connkey func prepare(conn n ......
Go - Wrapping an Error with Other Errors
Problem: You want to provide additional information and context to an error you receive before returning it as another error. Solution: Wrap the error ......
Go - Creating Customized Errors
Problem: You want to create custom errors to communicate more information about the error encountered. Solution: Create a new string - based error or ......
Go - Simplifying Repetitive Error Handling
Problem: You want to reduce the number of lines of repetitive error - handling code. Solution: Use helper functions to reduce the number of lines of r ......
Go - Using Multiple Versions of the Same Dependent Packages
Problem: You want to use multiple versions of the same dependent packages in your code. Solution: Use the replace directive in the go.mod file to rena ......
Go - Requiring Local Versions of Dependent Packages
Problem: You want to use local versions of the dependent packages. Solution: Set up Go to use a vendor directory by running go mod vendor. Local versi ......
golang-select
select的作用 golang 中的 select 就是用来监听和 channel 有关的 IO 操作,当 IO 操作发生时,触发相应的动作。select 只能应用于 channel 的操作,既可以用于 channel 的数据接收,也可以用于 channel 的数据发送。如果 select 的多个 ......
Go每日一库之33:go-app
## 简介 [`go-app`](https://github.com/maxence-charriere/go-app)是一个使用 Go + WebAssembly 技术编写**渐进式 Web 应用**的库。WebAssembly 是一种可以运行在现代浏览器中的新式代码。近两年来,WebAssem ......
Go每日一库之29:gopsutil
## 简介 `gopsutil`是 Python 工具库[`psutil`](https://github.com/giampaolo/psutil) 的 Golang 移植版,可以帮助我们方便地获取各种系统和硬件信息。`gopsutil`为我们屏蔽了各个系统之间的差异,具有非常强悍的可移植性。有了 ......
Go每日一库之32:gron
## 简介 [`gron`](https://github.com/roylee0704/gron)是一个比较小巧、灵活的定时任务库,可以执行定时的、周期性的任务。`gron`提供简洁的、并发安全的接口。我们先介绍`gron`库的使用,然后简单分析一下源码。 ## 快速使用 先安装: ```bash ......
Go每日一库之34:zap
## 简介 在很早之前的文章中,我们介绍过 Go 标准日志库[`log`](https://go-quiz.github.io/2020/02/07/godailylib/log/)和结构化的日志库[`logrus`](https://go-quiz.github.io/2020/02/07/god ......
Go每日一库之35:zerolog
## 简介 每个编程语言都有很多日志库,因为记录日志在每个项目中都是必须的。前面我们介绍了标准日志库[`log`](https://go-quiz.github.io/2020/02/07/godailylib/log/)、好用的[`logrus`](https://go-quiz.github.i ......