[Golang] Operator

2021-10-01 hit count image

Let's see what operators can be used in Golang.

Outline

In this blog post, I will show you what operators can be used in Golang. You can see the full source code of this blog post on the link below.

Arithmetic operator

You can use the following arithmetic operators in Golang.

  • +: addition (integer, float, string)
  • -: subtraction (integer, float)
  • *: multiplication (integer, float)
  • /: division (integer, float)
  • %: reminder (integer)

Create the main.go file and modify it like the below to check the operators.

package main

import "fmt"

func main() {
    a := 6
    b := 5

    fmt.Println("Result:", a+b)
    fmt.Println("Result:", a-b)
    fmt.Println("Result:", a*b)
    fmt.Println("Result:", a/b)
    fmt.Println("Result:", a%b)
}

When you execute the above code, you will get the following result.

Result: 11
Result: 1
Result: 30
Result: 1
Result: 1

Bit operators

You can use the following bit operators in Golang.

  • &: AND bit operation (integer)
  • |: OR bit operation (integer)
  • ^: XOR bit operation (integer)
  • &^: bit clear (integer)

Modify the main.go file to check the bit operators.

package main

import "fmt"

func main() {
    a := 6
    b := 5

    fmt.Printf("Result: %08b\n", a&b)
    fmt.Printf("Result: %08b\n", a|b)
    fmt.Printf("Result: %08b\n", a^b)
    fmt.Printf("Result: %08b\n", a&^b)
}

When you execute the above code, you will get the following result.

Result: 00000100
Result: 00000111
Result: 00000011
Result: 00000010

Shift operators

  • <<: left shift (positive integer)
  • >>: right shift (positive integer)

Modify the main.go file to check the shift operators.

package main

import "fmt"

func main() {
    a := 6

    fmt.Printf("Result: %08b\n", a)
    fmt.Printf("Result: %08b\n", a<<2)
    fmt.Printf("Result: %08b\n", a>>1)
}

When you execute the above code, you will get the following result.

Result: 00000110
Result: 00011000
Result: 00000011

Comparison operators

  • ==: same
  • !=: different
  • <: less than
  • >: greater than
  • <=: less than or equal
  • >=: greater than or equal

Modify the main.go file to check the comparison operators.

package main

import "fmt"

func main() {
    a := 6
    b := 5

    fmt.Println("Result:", a == b)
    fmt.Println("Result:", a != b)
    fmt.Println("Result:", a < b)
    fmt.Println("Result:", a > b)
    fmt.Println("Result:", a <= b)
    fmt.Println("Result:", a >= b)
}

When you execute the above code, you will get the following result.

Result: false
Result: true
Result: false
Result: true
Result: false
Result: true

Logical operators

  • &&: AND operator
  • ||: OR operator
  • !: NOT operator

Modify the main.go file to check the logical operators.

package main

import "fmt"

func main() {
    a := true
    b := false

    fmt.Println("Result:", a && b)
    fmt.Println("Result:", a || b)
    fmt.Println("Result:", !a)
}

When you execute the above code, you will get the following result.

Result: false
Result: true
Result: false

Assignment operator

You can use the following assignment operators in Golang.

a = 1
a, b = 1, 2

You can use the following assignment operator to exchange the values of two variables.

a, b = b, a

Compound assignment operators

You can use the following compound assignment operators in Golang.

  • +=: a += 1 / a = a + 1
  • -=
  • *=
  • /=
  • %=
  • &=
  • |=
  • ^=
  • <<=
  • >>=

Increment/Decrement operators

  • ++: a ++ / a = a + 1
  • --

Increment/Decrement operators don’t return the value, so you can’t use them like the below.

b = a++

Operator precedence

Golang has the following operator precedence.

PriorityOperator
1*, /, %, <<, >>, &, &^
2+, -, |, ^
3==, !=, <, <=, >, >=
4&&
5||

Of course, the operator in the brackets((,)) is evaluated first.

Nextafter

Bits cannot accurately represent real numbers. So, when expressing a real number, the real number is expressed as an approximate number that is one bit larger or one bit smaller than the original number.

package main

import (
    "fmt"
    "math"
)

func main() {
    var a float64 = 0.1
    var b float64 = 0.2
    var c float64 = 0.3

    fmt.Printf("%0.18f\n", c)
    fmt.Printf("%0.18f\n", a+b)
    fmt.Printf("%0.18f == %0.18f (%v)\n", c, a+b, c == a+b)
}

When you write the code above and execute it, you will get the following result.

0.299999999999999989
0.300000000000000044
0.299999999999999989 == 0.300000000000000044 (false)

You can see that false is displayed differently from the expected result.

To solve this problem, you can use the Nextafter function in the math package.

fmt.Printf("%0.18f == %0.18f (%v)\n", c, a+b, c == math.Nextafter(a+b, c))

When you execute the code above, you will get the following result.

0.299999999999999989 == 0.300000000000000044 (true)

Nextafter gets two numbers and returns the value shifted by 1 bit from the first number toward the second number. Therefore, you can accurately compare the values of real numbers.

Completed

Done! we’ve seen what operators are in Golang. Almost all of them are used in the other languages, so if you already know other languages, you’ll feel easy about this part.

However, when you compare the real numbers, you should think about the error of the real number and use Nextafter to compare them.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts