_在Golang中是什么意思? [重复]
This question already has an answer here:
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)
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.
Here's a link to the thread I was looking at if it helps: How to check if a file exists in Go?
</div>
此问题已经存在 在这里有答案 p>
-
在Go声明中什么是“ _”(下划线逗号)?
7个答案
span>
li>
ul>
div>
我是Go的新手,遇到了 浏览其他线程时的以下代码行: p>
如果_,err:= os.Stat(“ / path / to / whatever”); os.IsNotExist(err) code> pre>
在if之后,_是什么意思? 它是否指定将在if条件中分配某些内容(因为err似乎正在发生这种情况)? 我在Wiki上找不到该语法的示例,我很好奇它的用途。 p>
这里是我正在查看的线程的链接(如果有帮助的话) : 如何检查Go中是否存在文件? p> div>
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.
The compiler will just throw that value away.