What "|=" operator means in Go?

huangapple go评论77阅读模式
英文:

What "|=" operator means in Go?

问题

我找到了这个运算符|=,我想知道它的含义。

func getPageInfoMode(r *http.Request) (mode PageInfoMode) {
    for _, k := range strings.Split(r.FormValue("m"), ",") {
        if m, found := modeNames[strings.TrimSpace(k)]; found {
            mode |= m
        }
    }
    return
}

这段代码中的|=是位运算符的一种使用方式,表示按位或赋值。它将右侧的值与左侧的变量进行按位或操作,并将结果赋值给左侧的变量。在这个例子中,mode |= m将变量m的值与mode进行按位或操作,并将结果赋值给mode变量。

英文:

I've found this operator |= and I'm wondering what it means

func getPageInfoMode(r *http.Request) (mode PageInfoMode) {
	for _, k := range strings.Split(r.FormValue("m"), ",") {
		if m, found := modeNames[strings.TrimSpace(k)]; found {
			mode |= m
		}
	}
	return
}

答案1

得分: 3

是的,位或操作符是一种原地操作符,用于按位或运算。在Go语言中,还有许多其他的操作符,包括加法、按位与、加法赋值、按位与赋值、逻辑与、等于、不等于、减法、按位或、减法赋值、按位或赋值、逻辑或、小于、小于等于、乘法、按位异或、乘法赋值、按位异或赋值、发送操作符、大于、大于等于、除法、左移、除法赋值、左移赋值、自增、赋值、声明赋值、取模、右移、取模赋值、右移赋值、自减、逻辑非、按位清除、按位清除赋值等等。

英文:

Is an inplace bitwise OR operator https://golang.org/ref/spec#Operators.

There are many others:

+    &     +=    &=     &&    ==    !=        
-    |     -=    |=     ||    <     <=        
*    ^     *=    ^=     <-    >     >=        
/    <<    /=    <<=    ++    =     :=        
%    >>    %=    >>=    --    !            
&^   &^=

huangapple
  • 本文由 发表于 2015年1月14日 08:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/27933739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定