为什么在Golang中使用 | 将多个值作为函数参数传递会起作用?

huangapple go评论69阅读模式
英文:

Why passing multiple values with | work as arguments to a function on Golang?

问题

我正在翻译以下内容:

我正在查看 os 包的文档,看到了这个代码:

f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

根据文档,OpenFile 的签名是:

func OpenFile(name string, flag int, perm FileMode) (*File, error)

为什么将 os.O_APPEND|os.O_CREATE|os.O_WRONLY 作为第二个参数传递进去可以正常工作?

英文:

I was looking at the os package docs and saw this:

f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

According to the docs the OpenFile signature its

func OpenFile(name string, flag int, perm FileMode) (*File, error)

Why does this works passing os.O_APPEND|os.O_CREATE|os.O_WRONLY as the second argument?

答案1

得分: 2

常量os.O_APPENDos.O_CREATEos.O_WRONLY是整数类型,因此这里对它们进行按位或操作,并将它们组合成一个单独的整数。

英文:

The constants os.O_APPEND, os.O_CREATE, and os.O_WRONLY are ints, so this is taking the bitwise OR operation on them and combining them into a single int.

huangapple
  • 本文由 发表于 2021年11月28日 05:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/70138932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定