这是什么意思? [英] What does _, mean?

查看:68
本文介绍了这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Go语言的新手,在浏览其他一些线程时遇到了以下代码:

I'm new to Go and came across this line of code while browsing through some other threads:

if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err)

_的含义是什么?它是否指定将在if条件中分配某些内容(因为err似乎正在发生这种情况)?我在Wiki上找不到这种语法的示例,我很好奇它的用途.

What does the _, after the if mean? Is it specifying that something will be assigned in the if condition (as it appears is happening with err)? I couldn't find an example of this syntax on the wiki and I'm very curious to see what it's used for.

这是我正在查看的线程的链接,是否有帮助: 如何检查Go中是否存在文件?

Here's a link to the thread I was looking at if it helps: How to check if a file exists in Go?

推荐答案

由于os.Stat返回两个值,因此如果需要它们,则必须在某个地方接收这些值. _是一个占位符,从本质上讲意味着我不在乎此特定的返回值".在这里,我们只关心检查错误,而无需对实际的FileInfo Stat给我们做任何事情.

Because os.Stat returns two values, you have to have somewhere to receive those if you want any of them. The _ is a placeholder that essentially means "I don't care about this particular return value." Here, we only care to check the error, but don't need to do anything with the actual FileInfo Stat gives us.

编译器将丢弃该值.

这篇关于这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆