Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does _, mean? [duplicate]

Tags:

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)

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?

like image 642
Ryan Fitzpatrick Avatar asked Feb 09 '23 09:02

Ryan Fitzpatrick


1 Answers

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.

like image 105
captncraig Avatar answered Feb 11 '23 23:02

captncraig