duanlan8763 2015-12-03 19:57
浏览 45
已采纳

_在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>
  • 写回答

1条回答 默认 最新

  • douxin2011 2015-12-03 20:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?