设为首页 加入收藏

TOP

Effection Go(一)
2017-09-30 13:49:39 】 浏览:7540
Tags:Effection

Introduction:

新语言, 新思维

Formatting

Indentation: 默认tab

Line Length: 无限制, 会自动换行

Parentheses: 圆括号, 无限制, 但会自动去掉if, switch, for控制结构中的圆括号.

使用gofmt命令自动格式源码.

Commentary

多行: /**/

单行: //

使用godoc命令自动导出注释. 文档注释指紧邻API的注释, 如果遇到空行则会中止.

Names:

package names

interface names: 一般以-er或-able结尾

MixedCaps:

即Java中的驼峰式

GO中的identifier由字线,数字,下划线_组成, 并且开头不能是数字. 可分3种:

* exported identifier: 大写开头

* package identifier: 小写或下划线_开头

* blank identifier: 下划线_

Semicolons:

GO自动在行尾插入semicolon, 如果最后token是:

* identifier

* literal

* one of break continue, fallthrough return ++ -- ) ] }

根据解析规则, 控制结构的块开始标记{ 另起一行.

Control structures

if-else: 与传统C语言相同

redeclaration and reassignment: 使用short declaration必须保证at least one other variable is anew. 至少一个新变量

for: 有4种形式,

* for{...}, 死循环

* for condition {...}, 条件循环

* for init; condition; post {...}, 带初始条件后置循环, 其中init部分只能使用短声明定义新变量.

* for x := range xxx{...}, range循环

switch: 有2种形式,

* switch {...}, 非匹配选择

* switch expression {...}, 是匹配选择

select: 有1种形式,

* select {}, 非匹配选择

Type switch:

Go的类型检测与转换有type-assertion与type-switch. type-switch指x.(type)必须结合switch expression{}使用. 否则报错! 另一种替换用法是relect.TypeOf()

Funtions:

Multiple return values: 多值返回

Named result parameters: 命令返回参数

注意: Go中的method都是function(receiver, params...)形式. 但反之则不是. 另外, 只有本地类型才能定义method. cannot define new methods on non-local type. 所谓local type指在相同package path.

Defer:

defer, panic(), recover()配合使用.

理解Go的function机制, 需要明白defer stack, panic error, return value.

function无论正常结束或panic结束,都保证执行defer stack中的函数.

function如果正常结束或recover恢复panic error, 则会return value. 否则抛出panic error.

function执行defer stack时, 新的panic error会覆盖旧的panic error.

总结而言, function是Go基本的执行处理单元.

Data:

Allocation with new or make: 理解Go的new()与make()用法, 需要明白GO的数据类型知识.

* 根据功能划分:

基本类型: bool, int/uint/unitptr, float, complex, byte/rune/string, struct, array

指针类型: slice, map, chan, func, pointer,

接口类型: interface, error

当然nil类型也可以有pointer形式, 但不是GO的idiomatic. 接口类型是一种特殊的类型, 由<type,value>复合而成. 因为当且仅当<nil, nil>才是nil, 所以nil interface不一定等于nil.

* 根据零值(zero-value)划分:

基本类型的零值都是non-nil

指针类型与接口类型的零值都是nil. 其实说接口类型的零值是nil不大恰当,因为interface是由<type,value>组成, 如果type为基本类型,则value的零值就不会是nil了. 指针类型与接口类型也有指针形式, 这相当于C中指针的指针, 通过间接指针来访问数据不会是个理想的做法.

所以, 你不会用new()来创建指针类型的指针, 因为其零值是nil, 创建一个指向nil的间接指针有什么意义哦? 也不能用make()来创建基本类型的值,因为其拥有零值了.再初始化就无价值了. 至于接口类型比较奇葩, 其底层是<type,value>,且仅当<nil,nil>时才等于nil. 否则要用 x == nil || reflect.ValueOf(x).IsNil()来判断是否为nil.

Printing:

Go中print有2种选择, 一是builtin的print(), println()函数. 一是fmt package. 据说后者带有缓冲, 性能更优.

Builtin:

GO中builtin函数:

append(), copy(), delete()

len(), cap(), close()

new(), make()

complex(), real(), imag()

print(), println()

panic(), recover()

import, type, const, var block:

Go中可以将多条import, type, const, var组合为block.

在const block中使用iota表示line index(从0开始)

关于var, const的没有声明或定义:

变量的类型向后追溯, 直到第一个类型.

常量的定义向前追溯, 直到第一个定义.

init function:

每个go package(或者说go file)可以定义零个或多个init()函数. 在import package时会被执行, 但共执行顺序无保证. 注意: 每次import package都会执行init函数, 而不仅仅初次导入执行. 这与Java中的<clinit>方法不同.

Pointers vs Values:

对于类型T, T的method是receiver为T的method. *T的method是receiver为T或*T的method. 简单地说, T的method属于*T, 但反之不成立.

顺便记忆: cannot define new methods on non-local type.

Interface and methods:

interface是method的集合. struct是data的集合.

interface类型转换使用type-assertion与type-switch.

Im

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇怎样开始GO编程? 下一篇golang 远程传输文件

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目