A Note of Effective Go (First Half)

Formatting

gofmt对源文件格式化(用go fmtpackage格式化)

Commentary

支持/* */风格的块注释和//风格的行注释。文档注释开头最好用函数名(便于grep时找到函数名)。缩进的文本采用等宽字体,适用于代码片段。

Names

导出符号需要将其首字母大写。

  • Package names
    包名是应当简短、具体、形象的,通常是小写和单个词,因此不需要下划线和混合大小写。因为需要包名做前缀,所以包内导出的名字可以简短些,比如用bufio.Reader代替bufio.BufReader
  • Getters
    Get方法的名字里不需要加Get
  • Interface names
    单个方法的接口用带-er的后缀或者类似的方式命名成代理人名词,比如Reader, Writer, Formatter, CloseNotifier等。
  • MixedCaps
    MixedCapsmixedCaps而不是下划线。

Semicolons

C一样Go也用分号来分隔语句,但它会根据规则(在终结语句的token和新行之间)自动插入分号。

Control structures

复用已声明的变量的实用规则,比如重用下面的err变量

f, err := os.Open(name) // 声明f和err
d, err := f.Stat()      // 声明d,重赋值err

Switch语句支持逗号分隔的列表case ' ', '?', '&', '=', '#', '+', '%':

Type switch

变量自动拥有每个子句对应的类型

var t interface{}
t = functionOfSomeType()
switch t := t.(type) {
default:
    fmt.Printf("unexpected type %T", t)       // %T prints whatever type t has
case bool:
    fmt.Printf("boolean %t\n", t)             // t has type bool
case int:
    fmt.Printf("integer %d\n", t)             // t has type int
case *bool:
    fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool
case *int:
    fmt.Printf("pointer to integer %d\n", *t) // t has type *int
}

Functions

  • Multiple return values
  • Named result parameters
  • Defer
    调用参数在defer执行时被求值,而非函数调用时。并且被延迟的函数按照LIFO的顺序执行。

Data

  • Allocation with new
    new用零值初始化所有类型。
  • Constructors and composite literals
  • Allocation with make
  • Arrays
  • Slices
  • Two-dimensional slices
  • Maps
    可以用作key的包括integers, floating point and complex numbers, strings, pointers, interfaces (as long as the dynamic type supports equality), structs and arrays等定义了相等性的类型。
// Create a timezone
var timeZone = map[string]int{
    "UTC": 0 * 60 * 60,
    "EST": -5 * 60 * 60,
    "CST": -6 * 60 * 60,
    "MST": -7 * 60 * 60,
    "PST": -8 * 60 * 60,
}
offset := timeZone["EST"]   // Get value
seconds, ok := timeZone[tz] // Get value and present
_, present := timeZone[tz]  // Get present
delete(timeZone, "PDT")     // Delete key
  • Printing
    在格式化字符串中根据数据类型而不是flag来决定有无符号和位数。
  • %v (for "value")用来指定任何值的默认格式,跟PrintPrintln的输出一样。
  • %+v在输出结构体时,还附带上每个域的名字。
  • %#v输出完整的Go语法。
  • %q应用到string[]byte类型上输出带引号的字符串。%#q则尽可能用反引号。
  • %x应用到strings, byte arrays, byte slices, integers生成长长的十六进制字符串。% x(有个空格)在字节间加上空格。
  • %T用来输出值的类型。
    可变参数使用...来指定
// Println prints to the standard logger in the manner of fmt.Println.
func Println(v ...interface{}) {
    std.Output(2, fmt.Sprintln(v...))  // Output takes parameters (int, string)
}
// We write ... after v in the nested call to Sprintln to tell the compiler to treat v 
as a list of arguments; otherwise it would just pass v as a single slice argument.
  • Append
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,068评论 19 139
  • 01.{ 换行: Opening Brace Can't Be Placed on a Separate Lin...
    码农不器阅读 7,024评论 0 14
  • 标签(空格分隔): 编程 Go官方文档 Using the tour 1.1 Hello, 世界 Welcome...
    uangianlap阅读 5,451评论 0 5
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,006评论 18 399
  • 前言 人生苦多,快来 Kotlin ,快速学习Kotlin! 什么是Kotlin? Kotlin 是种静态类型编程...
    任半生嚣狂阅读 26,516评论 9 118

友情链接更多精彩内容