登录
首页 >  Golang >  Go问答

Go 语言中的管道运算符是什么?

来源:stackoverflow

时间:2024-02-20 16:54:25 325浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《Go 语言中的管道运算符是什么?》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

这个 | 运算符在 go 中做什么?我在

中找到了这个
import log

log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)

当我检查 log.setflags(flag) 方法时,它接受 int。我不明白它如何对这个 int 值进行操作?


解决方案


| 运算符是 bitwise or,如 the spec 的算术运算符部分中所述。

这对两个整数执行按位 or 操作。在本例中,将多个标志合并为一个。

log包中,标志具有以下值:

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
  • l微秒:4(或b00100
  • llongfile:8(或b01000

对这三个值执行按位或运算,得到 b01101 或 13。这是使用“位标志”并将它们组合起来的常见方法。

| 运算符是一个称为 bitwise 的算术运算符,使用 or用于整数运算。

示例

var a uint = 60  /* 60 = 0011 1100 */  
   var b uint = 13  /* 13 = 0000 1101 */ 
   c := a | b       /* 61 = 0011 1101 */

在这里, log.ldatelog.lmicrosecondslog.llongfile 均表示 int 值。 它们的值按位或意味着 1|4|8 = 13,因此标志设置为 13,这是一个 int 值。

今天关于《Go 语言中的管道运算符是什么?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>