Go: Operators (Part 3)

This is a third post on Go programming language. You can read the first post here and second post here.

Go is rich in terms of supporting operators. Go has supports for all arithmetic operators including +, -, *, /, and %. Using these operators, you can build expressions which do arithmetic operations. For example, to count simple interest you can write the following program si.go with these operators.

package main

import "fmt"

func main() {
    var p, r, n float64 = 1000, 10, 1
    var si float64

    si = p * r * n / 100

    fmt.Println("The simple interest is", si)
}

The operator of precedence is normal as in other programming languages. Also, pay attention to the types of all variables. All variables in calculation must be in the same type otherwise it will generate an error.

Go has support for relational operators such as ==, !=, <, |, <=, >, and >=. We'll use these operators when we talk about conditionals.

Go has support for logical operators that includes ||, && and ! for or, and and negation respectively. Go heavily used these symbols to define some sort of operations. We'll take about all possible operators in future whenever needed.

With these operators, you can find the area of circle as written in area.go.

package main

import "fmt"

func main() {
    var pi, r float64 = 3.14, 10

    var area float64 = pi * (r * r)

    fmt.Println("The area of cirlce is", area)
}

If we run the above program, it will produce the following output.

$ go run area.go 
The area of circle is 314

In this program, I used the parenthesis to override the precedence. The parenthesis are also useful to convey the message in formula that first, I want to square the radius and then to multiply it with pi.

Let's write another program that calculate speed based on distance d and take time t in speed.go.

package main

import "fmt"

func main() {
    var d, t float64 = 125, 3
    var s int

    s = d / t

    fmt.Println("The speed was", s)
}

Now, if I run this program, it will produce following output.

$ go run speed.go
# command-line-arguments
./speed.go:9:4: cannot use d / t (type float64) as type int in assignment

We got the error. Why? As I mentioned earlier that all the variables involved in operation must be in same type otherwise it will not work. Go will not auto convert your smaller type into bigger one like in this case if we are assuming that Go will convert integer to float because output is in float. In this program, the type of s is causing issue. Can you solve this error?

If so please do it and then floor is open for questions.

Can I know the operator of precedence?: You can search on Internet. My personal suggestion is to use parenthesis always instead of remembering operator of precedence. Because, your program is going to read by many other programmers.

Don't get me wrong, but these are really basics examples: I know. But, still these will help you with practise. Also, in order to write a little complex examples, I have to use other programming constructs such as conditionals, loops or may be some functions and we haven't talked about these, right. So, basic examples for now.

Naming the variables like s, si, p are really bad practise. Why you are doing such a practise?: Did you face any problem on understanding the meaning of these single character variables? I mean you easily know that s in last program is speed and r in first program is rate. If proper context is given, you can easily understand the complete program. Personally, we do not have to complicated the things if not needed. I can write a comment that convey the message. Speaking about comments - In Go, you can write two type of comments - single-line comments and multi-line comments. Single-line comment can be written using // and multi-line comment can be written using /* and */. For example,

/* filename: speed.go
This program calculates speed (s) from distance (d) and taken time (t)
*/ 

package main

import "fmt"

func main() {
    var d, t float64 = 125, 3
    var s int

    s = d / t //Standard formula to calculate the speed.

    fmt.Println("The speed was", s)
}

Rhett Trickett picture

I'm enjoying how this is progressing. Nice, easy to digest posts. The type error can be corrected by setting s to be the same type as d and t. By the way, you can also past repl.it urls into the editor to embed runnable code samples, if you'd like.