大话西游666
空白标识符有许多可能的用途,但是其主要目的是允许丢弃具有多个返回值的函数的返回值:// We only care about the rune and possible error, not its lengthr, _, err := buf.ReadRune()还有其他一些有趣的用法,但有时会令人讨厌。将导入或局部变量标记为“已使用”,以便编译器不会发出错误:import "fmt"var _ = fmt.Println // now fmt is used and the compiler won't complainfunc main() { var x int _ = x // now x is used and the compiler won't complain}确保类型在编译时实现接口:var _ InterfaceType = Type{} // or new(Type), or whatever确保常量在编译时位于一定范围内:// Ensure constVal <= 10const _ uint = 10 - constVal// Ensure constVal >= 1 (constVal > UINT_MAX + 1 is also an error)const _ uint = -1 + constVal确保未使用功能参数:// This could be useful if somebody wants a value of type// func(int, int) int// but you don't want the second parameter.func identity(x, _ int) int { return x }