0%
Go Engineering - Foundation - CLI
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
16k
Reading time ≈
15 mins.
Go Engineering - Foundation - Log - Package
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
5.8k
Reading time ≈
5 mins.
Go Engineering - Foundation - Log - Design
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
3.1k
Reading time ≈
3 mins.
Go Engineering - Foundation - Error - Package
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
12k
Reading time ≈
11 mins.
功能需求
- 支持错误堆栈
- 支持不同的打印格式,例如
%+v
、%v
、%s
等 - 支持 Wrap/Unwrap 功能:在已有 error 的基础上,追加一些新的信息
errors.Wrap(err, "open file failed")
- 调用 Wrap 时,会生成一个错误堆栈节点
- 支持 Is 方法:判断某个 error 是否为指定的 error
- Go 1.13 之前,并没有 wrapping error
if err == os.ErrNotExist {}
- 有 wrapping error 后,直接用
==
判断会有问题,因为可能是 wrapping errorfunc Is(err, target error) bool
- err 和 target 是同一个
- 当 err 是 wrapping error 时,target 包含在这个嵌套 error 链中
- Go 1.13 之前,并没有 wrapping error
- 支持 As 函数
- Go 1.13 之前,并没有 wrapping error,可以使用 type assertion 或者 type switch
if perr, ok := err.(*os.PathError); ok {}
- 有 wrapping error 时
var perr *os.PathError
if errors.As(err, &perr) {}
- Go 1.13 之前,并没有 wrapping error,可以使用 type assertion 或者 type switch
- 支持两种错误创建方式
errors.New("file not found")
errors.Errorf("file %s not found", "iam-apiserver")
Go Engineering - Foundation - Error - Code
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
2.5k
Reading time ≈
2 mins.
设计方式
场景:用户账号没有找到
200
HTTP Code 通常代表的是 HTTP Transport 层的状态信息;但对性能有一定的影响,因为需要解析 HTTP Body
1 | { |
Go Engineering - Foundation - API Doc
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
5.5k
Reading time ≈
5 mins.
Go Engineering - Foundation - Linting
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
5.1k
Reading time ≈
5 mins.
golangci-lint 优点
- 速度快
- 基于 gometalinter 开发,平均速度比 gometalinter 快 5 倍
- 并行检查代码 + 复用 go build 缓存 + 缓存分析结果
- 可配置
- 支持 YAML 格式的配置文件
- IDE 集成
- VS Code + Goland
- Linter 聚合器
- 集成了很多 Linter,无需单独安装,并且支持自定义 Linter
- 最小误报数
- 调整了所集成的 Linter 的默认设置,大幅减少误报
- 良好的输出
- 检查出问题的源码文件、行号和错误行内容
- 不符合检查规则的原因
- 报错的 Linter
- 更迭速度快
- 不断有新的 Linter 被集成进来
- 使用者:Google、Facebook、Istio、Red Hat OpenShift 等
Go Engineering - Foundation - RD
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
10k
Reading time ≈
9 mins.
Go Engineering - Foundation - Makefile
Posted on
In
Cloud Native
,
Cloud Native Foundation
,
Go Engineering
Symbols count in article:
10k
Reading time ≈
9 mins.
使用
- 先编写 Makefile 文件,指定整个项目的编译规则,然后通过 Linux make 命令来解析该 Makefile 文件,实现自动化
- 默认情况下,make 命令会在当前目录下,按照 GNUmakefile、makefile、Makefile(推荐)的顺序查找
make -f golang.mk
或者make --file golang.mk
规则
- 规则一般由目标、依赖和命令组成,用来指定源文件编译的先后顺序
- Makefile 规则可以自动判断是否需要重新编译某个目标,从而确保目标仅在需要时编译
规则语法
主要包括:target、prerequisites 和 command
1 | target ...: prerequisites ... |