亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

什么是“|” Go 中的運算符?

什么是“|” Go 中的運算符?

Go
米脂 2022-06-06 15:28:16
這個|運算符在 Go 中做了什么?我發現這個import loglog.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)當我檢查該log.SetFlags(flag)方法時,它接受一個int. 我不明白它是如何在這個int值上運行的?
查看完整描述

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。這是使用“位標志”并將它們組合起來的常用方法。


查看完整回答
反對 回復 2022-06-06
?
弒天下

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 值。


查看完整回答
反對 回復 2022-06-06
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號