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

Time:11-28

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?

CodePudding user response:

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.

  •  Tags:  
  • go
  • Related