2 回答

TA貢獻1712條經驗 獲得超3個贊
|運算符是,如規范的bitwise OR算術運算符部分所述。
這將按位執行OR兩個整數。在這種情況下,將多個標志組合為一個。
在日志包中,標志具有以下值:
const (
Ldate = 1 << iota // the date in the local time zone: 2009/01/23
Ltime // the time in the local time zone: 01:23:23
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
Llongfile // full file name and line number: /a/b/c/d.go:23
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
Lmsgprefix // move the "prefix" from the beginning of the line to before the message
LstdFlags = Ldate | Ltime // initial values for the standard logger
)
Ldate: 1 (或b00001)
Lmicroseconds: 4 (或b00100)
Llongfile: 8 (或b01000)
對所有三個執行按位或運算得到b01101或 13。這是使用“位標志”并將它們組合起來的常用方法。

TA貢獻1818條經驗 獲得超8個贊
|運算符是一個算術運算符,稱為bitwise OR用于整數運算。
例子
var a uint = 60 /* 60 = 0011 1100 */
var b uint = 13 /* 13 = 0000 1101 */
c := a | b /* 61 = 0011 1101 */
這里, log.Ldate, log.Lmicroseconds,log.Llongfile都代表int值。它們的值按位或表示1|4|8 = 13,因此標志設置為13int 值。
- 2 回答
- 0 關注
- 139 瀏覽
添加回答
舉報