variables, constants and operators IN GO

v

consider the program:

package main

import "fmt"

func main() {
	fmt.Println("value of", 5, "is", 5)
	fmt.Println("value of", 5, "+ 1", "is", 5+1)
	fmt.Println("value of", 5, "+ 2", "is", 5+2)
}

Nothings special here, the output should be as expected

This program is completely fine. Except its not maintainable. Here you can see all the operations we did is based on the fact that the main value we’re going to change is 5

which is ok but when we want to change the value to 7 per se. We have to re-write the entire code like this:

package main

import "fmt"

func main() {
	fmt.Println("value of", 7, "is", 7)
	fmt.Println("value of", 7, "+ 1", "is", 7+1)
	fmt.Println("value of", 7, "+ 2", "is", 7+2)
}

It takes time to rewrite this. Doesn’t it? So here comes the variable!

First lets rewrite this

package main

import "fmt"

func main() {
	fmt.Println("value of", a, "is", a)
	fmt.Println("value of", a, "+ 1", "is", a+1)
	fmt.Println("value of", a, "+ 2", "is", a+2)
}

Now here I changed the value 5 or 7 to something called a. I decided to choose ‘a’, you can change it to whatever you want. The idea is now its not a value anymore, its a variable.

But obviously now you will ask “ok, so whats the value does a have?”

Now? It has nothing. In fact GO can’t even say its a variable. We have to say ‘a’ is a variable to it.

The syntax in GO to say something is a variable is

var variable_name optional_data_type;

In our case its var a int.

So now our code looks like this

package main

import "fmt"

func main() {
	var a int

	fmt.Println("value of", a, "is", a)
	fmt.Println("value of", a, "+ 1", "is", a+1)
	fmt.Println("value of", a, "+ 2", "is", a+2)
}

So now if you run it.

This is not what we want. But we’re going somewhere here.

You see all the ‘a’ is replaced by 0. This is not a coincidence.

When you just declared a INTEGER variable, the default value of it is 0

You can read more about this default value here. https://golang.org/doc/

Now lets give it a value of our own not 0. To give it a value its simple. You have to remember only one thing.

  1. Declare a variable
  2. Give a value

You can do either var a int = 5 or

var a int
a = 5

Both works.

Here we know the value we’re going to say is INTEGER. But what if it can be a decimal, or it can be a ‘character’.

Lets say the value of a is not 5 but 5.0. Of course both are same to us but not for GO.

One is a integer other is a FLOAT. So we have to change it to var a float = 5.0

But most of the time, even we’re not sure of our variable type(data type). Like maybe you get a data from a sever using AJAX request, etc

In those cases, you can do something like this varaiable_name := value

Cool. Right?

Now we can change our code to anything like this

Now we got something like a super power.

Constants

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.

Constants can be of any of the basic data types likeĀ an integer constant, a floating constant, a character constant, or a string literal.

like value of pi is 3.14 or value of g (gravitational acceleration constant) is 9.8.

Of course we can declare it like a variable and run the program. Sure it works.

But what if one of your ‘engineering’ friend come and change your code to something like this

lol.

We have to say its not a variable, but a constant.

package main

import "fmt"

func main() {
	const pi float32 = 3.14
	const g float32 = 9.8
	
	fmt.Println("value of pi is",pi)
	fmt.Println("value of g is",g)
}

so now when your friend come and change your code to something like this

package main

import "fmt"

func main() {
	const pi float32 = 3.14
	const g float32 = 9.8

	pi = 3
	g = 9
	
	fmt.Println("value of pi is",pi)
	fmt.Println("value of g is",g)
}

GO should throw a error, something like this:

operators

We already used operators without even knowing it. Check out this previous code

package main

import "fmt"

func main() {
	a := 5

	fmt.Println("value of", a, "is", a)
	fmt.Println("value of", a, "+ 1", "is", a+1)
	fmt.Println("value of", a, "+ 2", "is", a+2)
}

Here we printed a, a+1, a+2. we used + operator to achieve this. You can also use other operators like +, -, /, % etc

Note: / is used to return quotient, % is used to return reminder

There are also something called boolean operators. But I think I should dedicate a whole article to explain Boolean and its operators.

So I hope you enjoyed it. Bye.

About the author

vigneshwar

Add comment

Leave a Reply

By vigneshwar

Most common tags

%d bloggers like this: