您的位置 首页 golang

简单介绍Go 中的空白标识符下划线

1.什么是空白标识符

空白标识符是未使用的值的占位符。它由下划线(_)表示。由于空白标识符没有名称,因此它们也被称为匿名占位符。

在 Go 语言中, 不允许声明未使用的变量或导入语句。也就是说,我们无法声明变量并将其留下未使用。同样,如果您导入一个包,那么也必须使用它。

这个时候就需要一个空白标识符。

2.未使用的赋值变量

如果 Go 中的函数返回多个值,则必须定义相等数量的变量来保存这些值。但是,如果您只需要其中一些值而不需要其他值,若某次赋值需要匹配多个左值,但其中某个变量不会被程序使用, 那么用空白标识符来代替。该变量可避免创建无用的变量,并能清楚地表明该值将被丢弃。

如下所示:

 result, error = Dosomething()
if error {
// handle error
}  

在上面的代码中,​result​​ 与程序无关,而且后续代码也没有使用它。如果编译的话,Go 编译器会报错。

 result declared but not used  

在这种情况下,您可以将结果替换为下划线或空白标识符,

如下所示:

 _, error = Dosomething()
if error {
// handle error
}  

即使您声明了一个变量,您也可以稍后使用空白标识符来忽略它,如下面的代码所示:

 product, error = Dosomething()
if error {
// handle error
}
// ignore unused variable
_ = product  

在 ​​for…range​​ 中使用空白标志符

 func Sum(numbers []int) int {
 
sum := 0
for _, number := range numbers {
sum += number
}
return sum
}  

3.未知的结构体

可以利用 ​​_​​ 分配任何东西:

 var _ I = T{}  

无法访问此变量,以便将从生成的程序中优化。但是,如果 T 类型未分配给接口 ​​I​​ ,则可能会导致编译错误。因此,在这种情况下,它被用作关于类型的静态断言。

4.未使用的包

在 Go 中,如果你导入一个包,那么你必须使用它,否则会出现编译器错误。

 imported and not used “”  

空白标识符可用于解决此编译器错误。

有两种方法可以使用空白标识符来解决未使用的包错误,如下所示:

第一种:声明一个全局空白标识符(在 ​​main()​​ 函数之前),该标识符从未使用的包中访问符号,例如下面代码中的 Open:

 import "os"
var _ = os.Open
func main() {
}  

第二种:在未使用的包前加上一个空白标识符,

如下所示:

 import _ "os"
 
func main(){
}  

空白标识符通过避免在整个代码中使用未使用的变量声明,使代码更具可读性。

5.总结

Golang 中任何声明但未使用的包和变量都会引发错误。这一规则使得我们的代码保持干净和轻量。

有时如果需要在未来使用并提前导入一些软件包,就可以使用空白标识符。

有时候函数会返回多个参数,但是我们只需要使用其中一个变量的时候,就可以通过空白标志符来进行接收。

到此这篇关于Go 中的空白标识符下划线的文章就介绍到这了。

文章来源:智云一二三科技

文章标题:简单介绍Go 中的空白标识符下划线

文章地址:https://www.zhihuclub.com/86366.shtml

关于作者: 智云科技

热门文章

评论已关闭

24条评论

  1. ICI 182, 780 ICI, a complete ER antagonist, blocked this E 2 effect and restored the TGF ОІ1 response on the reporter construct data not shown

  2. I wouldn t have missed it for anything How do you sell them said Jack to the tall boy who stood with his back to him, busied with the berries

  3. Mukherjee walks us through the development of radical surgeries to remove tumors; the realization that cancer was is a systemic illness; the use of X rays and radiation, the search for selective poisons anti cancer drugs such as Tamoxifen, Herceptin and Gleevec the important strides in palliative care when it became evident in the 1980s that the disease was stronger than our ability to cure it

  4. On a practical level, universities can educate managers about appropriate adjustments to accommodate menopause symptoms

  5. The widely prescribed pain drug pregabalin brand name Lyrica may be no better than a placebo when it comes to treating the back and leg pain known as sciatica, a new clinical trial suggests

  6. Each mL of the 30 mg vial contains, 3 mg Pamidronate Disodium, 47 mg Mannitol, USP; Water for Injection, USP, q

  7. Feng is Leiqi was about to arrive, and Zhao Ling also raised his hand to take this Anderson does doxycycline affect blood sugar move

  8. Downregulation of GAD65 might ensure faithful transmission of CGRP induced inflammatory pain to the central nervous system by a weakened GABAergic inhibition

  9. It is responsible for insulin production endocrine pancreas and the manufacture and secretion of digestive enzymes exocrine pancreas leading to carbohydrate, fat, and protein metabolism

  10. Various types of competitive assays have thus been developed and implemented on automated in vitro diagnostic devices

  11. I met with a few different moms, and adoption arrangements all fell apart for one reason or another but thenWyatt came through

  12. Freedman JE Fas induced apoptosis by the activation of procaspase 8, which lead to a series of downstream events including activation of effector caspases, cleavage of several caspase substrates, and induced BID dependent mitochondria damage 48

  13. Increasing doses of tamoxifen treatment Because aerobic and anaerobic bacteria are frequently present in the same infected site, appropriate procedures for isolation and culture are necessary to keep from overlooking the anaerobes

  14. In both disorders, most of the ventricular filling occurs in early diastole, with a rapid decrease in right atrial pressure immediately after the opening of the tricuspid valve; therefore, a prominent y descent is noted in the jugular veins

  15. The secret of a good PCT for deca consists in continuing to take testosterone based steroids for about two weeks after ending the nandrolone cycle

  16. Subjects were followed from the index date until death from breast cancer or the end of the study period, whichever occurred first

  17. imipramine promethazine syrup ip 5mg uses in hindi These days, Arab Street has blossomed and has a wonderfully eclectic mix of carpet emporiums, individual and rather swish boutiques, and a terrific selection of restaurants

  18. In the off season, when a bodybuilder s bodyfat is higher, the problem may be masked if it s minor Due to aspiration of fluid, airway maintenance and ventilation are more important in drowning victims in cardiopulmonary arrest compared to victims sustaining cardiac arrest due to coronary artery disease

网站地图