MENU

Go 学习笔记:运算符

August 22, 2021 • Golang

Go 学习笔记:运算符

Go 支持的运算符和C语言是完全相同的

比如基础的四则运算

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 10:40:37
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b int = 5, 7
    fmt.Println(a + b)
    fmt.Println(a - b)
    fmt.Println(a * b)
    fmt.Println(a / b)
    fmt.Println(a % b)
}

当然还有自增自减

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 10:44:31
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b int = 5, 7
    a++
    fmt.Println(a)
    b--
    fmt.Println(b)
}

但是与C语言不同的是,自增自减只有a++的形式,++a的形式是不被允许的,同时,你必须要将其作为独立的语句使用

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 10:45:16
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b int = 5, 7
    fmt.Println(a++) // 需要作为独立的语句使用
    fmt.Println(--b) // 这种形式也是不被允许的
}

当然,C语言常用的关系运算符也是支持的

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 10:49:42
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b int = 5, 7
    fmt.Println(a == b)
    fmt.Println(a >= b)
    fmt.Println(a <= b)
    fmt.Println(a != b)
    fmt.Println(a > b)
    fmt.Println(a < b)
}

还有逻辑运算符

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 10:56:29
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b bool = true, false
    fmt.Println(a && b)
    fmt.Println(a || b)
    fmt.Println(a && !b)
}

值得注意的是,逻辑运算符两边都只能值bool类型的值,使用非布尔类型的值是会报错的

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 10:57:32
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b int = 1, 2
    fmt.Println(a && b)
    fmt.Println(a || b)
    fmt.Println(a && !b)
}

/*
# command-line-arguments
.\main.go:16:16: invalid operation: a && b (operator && not defined on int)
.\main.go:17:16: invalid operation: a || b (operator || not defined on int)
.\main.go:18:19: invalid operation: !b (operator ! not defined on int)
*/

同时,位运算也是可以的

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 11:00:54
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b int = 1, 2
    fmt.Println(a << 1) // 二进制左移一位
    fmt.Println(b >> 1) // 二进制右移一位
    fmt.Println(a | 1)  // 求或
    fmt.Println(a & 1)  // 求与
    fmt.Println(a ^ 1)  // 求异或
}

当然还有一些赋值符号

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 11:07:02
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a, b, tmp int = 2, 4, 0
    tmp = a
    a = b
    b = tmp
    fmt.Println(a, b, tmp)

    a -= tmp
    fmt.Println(a)

    a += tmp
    fmt.Println(a)

    a *= tmp
    fmt.Println(a)

    a /= tmp
    fmt.Println(a)

    a <<= tmp
    fmt.Println(a)

    a >>= tmp
    fmt.Println(a)

    a ^= tmp
    fmt.Println(a)

    a |= tmp
    fmt.Println(a)

    a &= tmp
    fmt.Println(a)
}

还有俩和指针相关的

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 11:10:17
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    var a *int // * 表示这是一个指针变量
    var b int = 12 

    a = &b // & 取地址符
    fmt.Println(*a, a, b)
}

运算优先级也可以直接参考C语言

优先级运算符
5* / % << >> & &^
4+ - | ^
3== != < <= > >=
2&&
1||
作者:NorthCity1984
出处:https://grimoire.cn/golang/go6.html
版权:本文《Go 学习笔记:运算符》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任