Go Engineering - Foundation - Linting
golangci-lint 优点
速度快
基于 gometalinter 开发,平均速度比 gometalinter 快 5 倍
并行检查代码 + 复用 go build 缓存 + 缓存分析结果
可配置
支持 YAML 格式的配置文件
IDE 集成
VS Code + Goland
Linter 聚合器
集成了很多 Linter,无需单独安装,并且支持自定义 Linter
最小误报数
调整了所集成的 Linter 的默认设置,大幅减少误报
良好的输出
检查出问题的源码文件、行号和错误行内容
不符合检查规则的原因
报错的 Linter
更迭速度快
不断有新的 Linter 被集成进来
使用者:Google、Facebook、Istio、Red Hat OpenShift 等
golangci-lint 命令
cache123456789$ golangci-lint cache statusDir: /Users/zhongmingmao/Library/Caches/golangci-lintSize: 1.9MiB$ golangci-lint cache clean$ ...
Go Engineering - Foundation - RD
功能需求
为 iamctl 新增 helloworld 命令,该命令向终端打印 hello world
开发阶段代码开发
选择 Git Flow:适用于大型非开源项目
创建分支
基于 develop 分支,新建一个功能分支 feature/helloworld
1$ git checkout -b feature/helloworld develop
branch 名要符合 Git Flow 的分支命名规范,会通过 pre-commit 的 githook 来确保分支名符合规范
123$ md5 ./githooks/pre-commit ./.git/hooks/pre-commitMD5 (./githooks/pre-commit) = 3324d20a738461f3b6347f9ce7dae6b6MD5 (./.git/hooks/pre-commit) = 3324d20a738461f3b6347f9ce7dae6b6
./.git/hooks/pre-commit123456789101112131415161718#!/usr/bin/env bashLC_ALL= ...
Go Engineering - Foundation - Makefile
使用
先编写 Makefile 文件,指定整个项目的编译规则,然后通过 Linux make 命令来解析该 Makefile 文件,实现自动化
默认情况下,make 命令会在当前目录下,按照 GNUmakefile、makefile、Makefile(推荐)的顺序查找
make -f golang.mk 或者 make --file golang.mk
规则
规则一般由目标、依赖和命令组成,用来指定源文件编译的先后顺序
Makefile 规则可以自动判断是否需要重新编译某个目标,从而确保目标仅在需要时编译
规则语法
主要包括:target、prerequisites 和 command
1234target ...: prerequisites ... command ... ...
target
可以是一个 object file,也可以是一个执行文件,还可以是一个标签
可以使用通配符,当有多个目标时,使用空格分隔
prerequisites:代表生成该 target 所需要的依赖项,当有多个依赖项时,使用空格分隔
command:代表该 target 要执行 ...
Go Engineering - Foundation - API - RPC
RPC
Client 通过本地调用,调用 Client Stub
Client Stub 将参数打包(Marshalling)成一个消息,然后发送这个消息
Client 所在的 OS 将消息发送给 Server
Server 接收到消息后,将消息传递给 Server Stub(或者 Server Skeleton)
Server Stub 将消息解包(Unmarshalling)后得到消息
Server Stub 调用服务端的子程序,处理完成后,将最终结果按照相反的步骤返回给 Client
gRPC概述
gRPC:google Remote Procedure Call
gRPC 是由 Google 开发的高性能、开源、跨语言的通用 RPC 框架,基于 HTTP 2.0,默认使用 Protocol Buffers 序列化
gRPC 的特点
支持多语言
基于 IDL(Interface Definition Language)文件定义服务
通过 proto3 生成指定语言的数据结构、服务端接口和客户端 Stub
通信协议基于标准的 HTTP/2,支持特性:双向流、消息头压缩、单 TCP ...
Go Engineering - Foundation - API - RESTful
概述
REST:Representational state transfer
REST 只是一种软件架构风格,是一组架构约束条件和原则,而不是技术框架
REST 有一系列规范,满足这些规范的 API 均可称为 RESTful API
REST 规范把所有内容都视为资源,对资源的操作对应 HTTP 协议提供的 GET、POST、PUT 和 DELETE 方法
由于 REST 与 HTTP 协议相辅相成,因此 HTTP 协议已经成为 RESTful API 的事实标准
特点
以资源为中心,一切都可以抽象成资源,所有行为都是对资源的 CRUD
资源对应着面向对象范式里面的对象
资源使用 URI 标识,每个资源实例都有一个唯一的 URI 标识
资源是有状态的,使用 JSON/XML 等在 HTTP Body 里表征资源的状态
客户端通过 HTTP Method 对资源进行操作,实现 REST
无状态:每个 RESTful API 请求都包含了所有足够完成本次操作的信息,服务器无需保持 Session
无状态对于服务端的弹性扩容是很重要的
设计原则
RESTful API 的核心是规范
...
Go Engineering - Specification - Design Pattern
GoF
创建型模式
提供一种在创建对象的同时隐藏创建逻辑的方式,而不是直接使用 new 运算符直接实例化对象
单例模式
分为饿汉方式(包被加载时创建)和懒汉方式(第一次使用时创建)
饿汉方式1234567891011package hungertype singleton struct {}// 实例是在包被导入时被初始化的var ins *singleton = &singleton{}func GetIns() *singleton { return ins}
懒汉方式
非并发安全,需要加锁
1234567891011121314151617181920package singletonimport "sync"type singleton struct {}var ins *singletonvar lock sync.Mutexfunc GetIns() *singleton { if ins == nil { lock.Lock() ...
Go Engineering - Specification - Design Method
Go 项目
Go 项目是一个偏工程化的概念,包含 Go 应用
Go 应用
代码结构按层拆分
最大的问题:相同功能可能在不同层被使用,而这些功能又分散在不同的层中,容易造成循环引用
12345678910├── controllers│ ├── billing│ ├── order│ └── user├── models│ ├── billing.go│ ├── order.go│ └── user.go└── views └── layouts
按功能拆分
Go 项目最常用的拆分方法
不同模块,功能单一,可以实现高内聚低耦合的设计哲学
所有功能只实现一遍,引用逻辑清晰,大大减少循环引用的概率
12345pkg├── billing├── order│ └── order.go└── user
代码规范编码规范
Uber Go 语言编码规范
静态代码检查工具:golangci-lint
最佳实践
Effective Go
Go Code Review Comments
Style guideline for Go packages
代码质量编写可测试 ...
Cloud Native - Security & Compliance - OPA - Philosophy
Overview
A policy is a set of rules that governs the behavior of a software service.
Authorization is a special kind of policy.
difference
Authentication: how people or machines prove they are who they say they are.
Authorization: which people or machines can run which actions on which resources.
Authorization and more generally policy often utilize the results of authentication (the username, user attributes, groups, claims), but makes decisions based on far more information than just who the user is.
...
Cloud Native - Security & Compliance - OPA - Introduction
Overview
The Open Policy Agent is an general-purpose policy engine that unifies policy enforcement across the stack.
OPA provides a high-level declarative language
that lets you specify policy as code and simple APIs to offload policy decision-making from your software.
OPA decouples policy decision-making from policy enforcement.
When your software needs to make policy decisions it queries OPA and supplies structured data (e.g., JSON) as input.
OPA accepts arbitrary structured data as input.
OPA genera ...
Cloud Native - Security & Compliance - OPA - Glance
Overview
Policy-based control for cloud native environments
Flexible, fine-grained control for administrators across the stack
Use OPA for a unified toolset and framework for policy across the cloud native stack.
Decouple policy from the service’s code, so you can release, analyze, and review policies without sacrificing availability or performance.
Declarative Policy
Declarative
Express policy in a high-level, declarative language that promotes safe, performant, fine-grained controls.
DSL : Use a langua ...