Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Operators

Operators combine operands into expressions. Go language is rich in built-in operators and provides the following types of operators:

Arithmetic Operators

Arithmetic operators apply to numeric values and yield a result of the same type as the first operand. The four standard arithmetic operators (+, -, *, /) apply to integer, floating-point, and complex types; + also applies to strings.

package main
import "fmt"
func main() {
    var m,n int = 100,25
    var a,b string = "John","Carry"
    var sum,difference,product,quotient,remainder int
    var sumstring string
    sum = m+n // +   sum   integers, floats, complex values, strings
    sumstring = a+" "+b
    difference = m-n // -   difference   integers, floats, complex values
    product = m*n // *   product   integers, floats, complex values
    quotient = m/n // /   quotient   integers, floats, complex values
    remainder = m%n //%   remainder  integers
    fmt.Println(sum)
    fmt.Println(sumstring)
    fmt.Println(difference)
    fmt.Println(product)
    fmt.Println(quotient)
    fmt.Println(remainder)
}

Bitwise Operators

The bitwise logical and shift operators apply to integers only. Bitwise operator works on bits and perform bit-by-bit operation.

package main
import "fmt"
func main() {
    var x,y,z int
    x = 75
    y = 25
    z = x & y		// &    bitwise AND            integers
    fmt.Println(z)
    z = x | y		// |    bitwise OR             integers
    fmt.Println(z)
    z = x ^ y		// ^    bitwise XOR            integers
    fmt.Println(z)
    z = x &^ y		// &^   bit clear (AND NOT)    integers
    fmt.Println(z)
}

Comparison operators

Comparison operators like == and < can also be used to compare a value of a named type to another of the same type, or to a value of the underlying type.

package main
import "fmt"
func main() {
    var num1 int = 50
    var num2 int = 60
    fmt.Println(num1==num2) // ==    equal
    fmt.Println(num1!=num2) // !=    not equal
    fmt.Println(num1num2)  // >     greater
    fmt.Println(num1>=num2) // >=    greater or equal
}

Logical Operators

Logical operators apply to boolean values and yield a result of the same type as the operands. The right operand is evaluated conditionally.

package main
import "fmt"
func main() {
    var num1 int = 50
    var num2 int = 60
    if(num1!=num2 && num1<=num2){ // &&  Called Logical AND operator.
        fmt.Println("True")
    }
    if(num1!=num2 || num1<=num2){ // ||  Called Logical OR Operator
        fmt.Println("True")
    }
    if(!(num1==num2)){ // !  Called Logical NOT Operator. Use to reverses the logical state of its operand.
        fmt.Println("True")
    }
}

Assignment Operators

Assignment statement that concatenates the old value of X with Y and assigns it back to X.
X=X+Y
X+=Y
The operator += is an assignment operator. Each arithmetic and logical operator like + or * has a corresponding assignment operator.

package main
import "fmt"
func main() {
    var X int = 50
    var Y int = 60
    X+=Y	// += Add AND assignment operator
    fmt.Println(X)
    X=50
    Y=60
    X-=Y   // -= Subtract AND assignment operator
    fmt.Println(X)
    X=50
    Y=60
    X*=Y  // *= Multiply AND assignment operator
    fmt.Println(X)
    X=4
    Y=44
    X%=Y  // %= Modulus AND assignment operator
    fmt.Println(X)
    X=50
    Y=200
    X/=Y  // /= Divide AND assignment operator
    fmt.Println(X)
}