Stay Hungry Stay Foolish

Alaways be curious to learn more and achieve more.

0%

Golang

  • Simplicity: 简单

    Simplicity is prerequisite for reliability. 简单是可靠的先觉条件

  • Readablity: 可读性

    Readability is essential for maintainablity. 可读性对于可维护性至关重要
    Programs must be written for people to read, and only incidentally for machine to execute. 程序必须是为人们阅读而编写的,而偶然为及其执行而编写

  • Productivity: 生产力

    Design is the art of arranging code to work today, and be changeable forever.

阅读全文 »

错误码设计

  • 错误码实现方式
    1
    2
    3
    4
    1. 无论请求成功或失败,始终返回200 http status code
    > 每次请求,都需要解析HTTP Body,从中解析出错误码和错误信息.
    2. 返回合适的HTTP Code. 并在Body中返回错误信息和自定义业务Code
    3. 返回合适的HTTP Code, 并在Body中详尽的错误信息
  • 业务Code码设计
    1
      

Swagger

Swagger是一套围绕OpenAPI规范构建的开源工具,可以设计·构建·编写·使用RESTAPI

  • Swagger编辑器:
  • Swagger UI: 将OpenAPI规范呈现为交互式API文档,并可以在浏览器中尝试API调用
  • Swagger Codegen: 根据OpenAPI规范,生成服务器存根和客户端代码库
阅读全文 »

Gin Web Framework

Gin是Go语言编写的Web框架,功能完善,使用简单,性能高,Gin核心的路由功能是通过HttpRouter实现,具有很高的路由性能.

阅读全文 »

Radix Tree - 基数树

radix tree用于表示一种空间优化的tree. 假如树中的一个节点是父节点的唯一子节点,那么该节点将会与父节点进行合并 radix tree的边沿edges可以是一个或者多个元素
元素个数不是太多,但是元素之间通常有很长的相同前缀时很适合采用radix tree存储
Golang的web框架gin使用radix tree作为路由查找的算法.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# gin/gin.go

// method -> HTTP Method {GET,PUT,POST,DELETE}
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
assert1(path[0] == '/', "path must begin with '/'")
assert1(method != "", "HTTP method can not be empty")
assert1(len(handlers) > 0, "there must be at least one handler")

debugPrintRoute(method, path, handlers)

// 每一个method对应一颗radix tree
root := engine.trees.get(method)
if root == nil {
// 如果获取不到method对应的树,就创建
root = new(node)
root.fullPath = "/"
engine.trees = append(engine.trees, methodTree{method: method, root: root})
}
root.addRoute(path, handlers)

// Update maxParams
if paramsCount := countParams(path); paramsCount > engine.maxParams {
engine.maxParams = paramsCount
}

Makefile

Makefile 关系到整个工程的编译规则(先后顺序)
make: 一个解释makefile中指令的命令工具

阅读全文 »

gRPC

Google开发共性能、开源、跨编程语言的通用RPC框架,基于HTTP2.0协议开发,默认采用Protocol Buffers数据序列化协议