go blank identifier _作用

hittata · · 1547 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

The blank identifier has many possible uses, but its main purpose is to allow discarding returns from functions that have multiple returns:

// We only care about the rune and possible error, not its length
r, _, err := buf.ReadRune()

There are some other fun, but sometimes hackish, uses.

Mark an import or local variable as "used" so that the compiler won't issue an error:

import "fmt"

var _ = fmt.Println // now fmt is used and the compiler won't complain

func main() {
    var x int
    _ = x // now x is used and the compiler won't complain

Make sure a type implements an interface at compile time:

var _ InterfaceType = Type{} // or new(Type), or whatever

Ensure that a constant lies within a certain range at compile time:

// Ensure constVal <= 10
const _ uint = 10 - constVal
// Ensure constVal >= 1 (constVal > UINT_MAX + 1 is also an error)
const _ uint = -1 + constVal

Ensure a function parameter is unused:

// 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 }
golang  sql/driver 里面有使用:
type noRows struct{}

var _ Result = noRows{}
这个就是让编译器静态的检查一个类型是否实现某个接口,这是一种编程技巧.golang的接口是duck模型的,没有显示的声明,某个具体类型松散的实现某个接口,通过这层检查,基本上我们就可以确定某个具体类型实现了某个接口.这也是golang duck接口模型的一个不好的点吧.这是一种辅助编程,加强程序健壮性的手段,理论上不使用这种方法,通过unit test也能保证.
Some people use a line like as an interface check. This line ensures that the type noRows implements the Result interface. If it doesn't you will get a compiler error.




查看原文:go blank identifier _作用

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1547 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传