Golang中下划线(_)的不错用法分享

Kefira ·
更新时间:2024-09-20
· 856 次阅读

目录

1、忽略返回值

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

3、用在import package

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

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中这样引入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

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

到此这篇关于Golang中下划线(_)的不错用法分享的文章就介绍到这了,更多相关Golang下划线内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



golang 下划线

需要 登录 后方可回复, 如果你还没有账号请 注册新账号