忽略返回值
这个应该是最简单的用途,比如某个函数返回三个参数,但是我们只需要其中的两个,另外一个参数可以忽略,这样的话代码可以这样写:
v1, v2, _ := function(...)
v1, _, _ := function(...)
如果没有产生新变量
_, _, _ = function(...)
_ = json.Unmarshal(...)
用在函数定义中省略带名的参数
func demo() ( int, int, string, int ,error)
返回多个参数时,尤其是类型相同的,不利于阅读,
func demo() ( sumA int, sumB int, _ string, total int ,_ error)
返回参数 int 加了名字,对比上面,便于理解,但是 "string" 和 "error"没有名字,编译会报错,用下划线可以忽略命名
用在变量(特别是接口断言)
例如我们定义了一个接口(interface):
type Animal interface {
Say()
}
然后定义了一个结构体(struct)
type Dog struct {
}
然后我们希望在代码中判断Dog这个struct是否实现了Animal这个interface
var _ Animal = Dog{}
上面用来判断Dog是否实现了Animal, 用作类型断言,如果Dog没有实现Animal,则会报编译错误
用在import package
假设我们在代码的import中这样引入package:
import _ "test/foo"
这表示在执行本段代码之前会先调用test/foo中的初始化函数(init),这种使用方式仅让导入的包做初始化,而不使用包中其他功能
例如我们定义了一个Foo struct,然后对它进行初始化
package foo
import "fmt"
type Foo struct {
Id int
Name string
}
func init() {
f := &Foo{Id: 123, Name: "abc"}
fmt.Printf("init foo object: %v\n", f)
}
然后在main函数里面引入test/foo
package main
import (
"fmt"
_ "test/foo"
)
func main() {
fmt.Printf("hello world\n")
}
运行结果如下
init foo object: &{123 abc}
hello world
我们可以看到:在main函数输出”hello world”之前就已经对foo对象进行初始化了!
版本校验,编译时断言
在使用gRPC的时候,看到通过 const 来做版本校验,因为他牵涉到多个项目多个包,生成的 pb.go 文件需要指定具体版本信息
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
常量 proto.ProtoPackageIsVersion4
在业务代码中不会被调用到,因此无法通过明确调用的方式来迫使编译器报错,因此可以再定义个常量来引用它
const _ = proto.ProtoPackageIsVersion4
或者
const vs4 = proto.ProtoPackageIsVersion4
比如,有些版本并未发生大的改动,无论是编译还是运行都不会有影响,但是运行结果不同,因此需要显示的做版本限制。