go - Pipe character in Golang

ID : 274469

viewed : 54

Tags : gobit-manipulationgo





Top 4 Answer for go - Pipe character in Golang

vote vote

93

As others have said, it's the bitwise [inclusive] OR operator. More specifically, the operators are being used to create bit mask flags, which is a way of combining option constants based on bitwise arithmetic. For example, if you have option constants that are powers of two, like so:

const (     red = 1 << iota    // 1 (binary: 001) (2 to the power of 0)     green              // 2 (binary: 010) (2 to the power of 1)     blue               // 4 (binary: 100) (2 to the power of 2) ) 

Then you can combine them with the bitwise OR operator like so:

const (     yellow = red | green          // 3 (binary: 011) (1 + 2)     purple = red | blue           // 5 (binary: 101) (1 + 4)     white = red | green | blue    // 7 (binary: 111) (1 + 2 + 4) ) 

So it simply provides a way for you to combine option constants based on bitwise arithmetic, relying on the way that powers of two are represented in the binary number system; notice how the binary bits are combined when using the OR operator. (For more information, see this example in the C programming language.) So by combining the options in your example, you are simply allowing the service to accept stop, shutdown and pause and continue commands.

vote vote

82

The Go Programming Language Specification

Arithmetic operators

|    bitwise OR             integers 

The Go programming language is defined in The Go Programming Language Specification.

vote vote

74

The | is not pipe character here,but or character,one of bit manipuations.

For example,1 | 1 = 1,1 | 2 = 3,0 | 0 = 0.

vote vote

70

Top 3 video Explaining go - Pipe character in Golang







Related QUESTION?