美文网首页
golang下划线(underscore)用法

golang下划线(underscore)用法

作者: yuff | 来源:发表于2023-02-11 12:01 被阅读0次

1.用在数字常量中

a:=100_10_1
fmt.Println(a)
//output:100101

面试遇到过,问编译会不会报错,答案是不会,程序中的数字可以使用下划线来进行分割(_)以便于为程序提供更好的可读性,Java里也有相同的语法:Underscores in Numeric Literals

2.用在import

import  _  "net/http/pprof"
import _ "github.com/go-sql-driver/mysql"

引入包只调用里面的init()函数,进行初始化

3.忽略变量的值

myslice:=[]int{}
for _, v := range myslice {}
a , _ , err := testFunc()//只需要返回值的部分值

type productImpl struct{}
var _ IProduct = new(productImpl)//IProduct 为接口 用于判断productImpl是否实现了该接口

相关文章

网友评论

      本文标题:golang下划线(underscore)用法

      本文链接:https://www.haomeiwen.com/subject/ouwlkdtx.html