【go语言】下划线

1.忽略返回值

这个应该是最简单的用途,比如某个函数返回三个参数,但是我们只需要其中的两个,另外一个参数可以忽略,这样的话代码可以这样写:

v1, v2, _ := function(...)
v1, _, _ := function(...)

如果没有产生新变量

_, _, _ = function(...)
_ = json.Unmarshal(...)

2.用在变量(特别是接口断言)

例如我们定义了一个接口(interface):

type Foo interface {
     Say()
}

然后定义了一个结构体(struct)

type Dog struct {

}

然后我们希望在代码中判断Dog这个struct是否实现了Foo这个interface

var _ Foo = Dog{}

上面用来判断Dog是否实现了Foo, 用作类型断言,如果Dog没有实现Foo,则会报编译错误

3.用在import package

 import 下划线(如:import hello/imp)的作用:当导入一个包时,该包下的文件里所有init()函数都会被执行,然而,有些时候我们并不需要把整个包都导入进来,仅仅是是希望它执行init()函数而已。这个时候就可以使用 import _引用该包。即使用【import _ 包路径】只是引用该包,仅仅是为了调用init()函数,所以无法通过包名来调用包中的其他函数.

假设我们在代码的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对象进行初始化了!

4.版本校验,编译时断言

在使用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

比如,有些版本并未发生大的改动,无论是编译还是运行都不会有影响,但是运行结果不同,因此需要显示的做版本限制。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容