在QCon上海2018大会上,David Cheney 讲师做了《Practical Go(上)》主题演讲,主要内容如下。
演讲简介:
Go is a language designed for engineering teams. It’s central themes are simplicity, readability, and maintainability. This talk will provide best practice real world advice for teams building projects in Go covering five areas; idiomatic code, package and api design, error handling, concurrency, and testing.
内容大纲
Identifiers
Choose identifiers for clarity, not brevity
Use a consistent declaraton style
Package Design
A good package starts with its name An identifier’s name includes its package name Prefer lower case package names and import paths
Rather than nesting deeply, return early
Make the zero value useful
Eschew package level state. No package level variables. Avoid global side effects.
Project Structure
Consider fewer, larger packages Arrange code into files by import statements. Prefer nouns for file names. Eschew elaborate package hierarchies, resist the desire to apply taxonomy
Keep package main small as small as possible
API Design
Design APIs that are hard to misuse. Design APIs for their default use case.
Prefer var args to []T parameters
Let callers define the interface they require
Prefer streaming interfaces
Use type assertions for optional behaviour
Error handling
Elminate handling by eliminating errors
Only handle an error once
Concurrency
Never start a goroutine without when it will stop. When sending or receiving on a channel, consider what happens if the other party never receives the message
Keep yourself busy while waiting for a goroutine. or, do the work yourself.
Leave concurrency to the caller
讲师介绍:
David Cheney
Heptio 资深工程师, 著名 Go 语言专家
David 是 Heptio 的资深工程师。Heptio 位于西雅图,致力于帮助开发者高效使用 Kubernetes。
David 是开源贡献者,Go 编程语言项目组成员。他经常在技术社区演讲,分享软件设计、性能和 Go 语言相关主题。
在加入 Heptio 之前,他曾在 Atlassian、Canonical 和澳大利亚政府任职,从事云基础设施相关的开发和运维工作。
完整演讲 PPT 下载链接:
https://qcon.infoq.cn/2018/shanghai/schedule
评论