Variables & Operators in Golang
In Golang (or Go), variables are used to store and manipulate data within programs. They are defined using the `var` keyword followed by the variable name and type. For example, `var age int` declares a variable named `age` of type `int`. Golang supports various data types including integers (`int`, `int8`, `int16`, `int32`, `int64`), floating-point numbers (`float32`, `float64`), strings (`string`), booleans (`bool`), and more.
Operators in Golang are symbols or keywords used to perform operations on variables and values. Golang supports arithmetic operators (`+`, `-`, `*`, `/`, `%`), comparison operators (`==`, `!=`, `<`, `<=`, `>`, `>=`), logical operators (`&&`, `||`, `!`), assignment operators (`=`, `+=`, `-=`, `*=`, `/=`), and more. For example, `a + b` performs addition, `x > y` checks if `x` is greater than `y`, and `!flag` negates the value of `flag`.
Golang also provides special operators like `&` (address operator), `*` (pointer dereference operator), `++` and `--` (increment and decrement operators), and `:=` (short variable declaration). Understanding variables and operators is essential for writing efficient and concise Golang code, enabling developers to manipulate data and perform computations effectively within their programs.
Prerequisites
Hardware : Local Machine
Software : VS Code, Golang
Variable Declaration
![](https://prwatech.in/blog/wp-content/uploads/2021/03/1-25.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/1output.jpg)
Variable declaration with var keyword.
![](https://prwatech.in/blog/wp-content/uploads/2021/03/2-26.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/2output.jpg)
Mixed variable declare
![](https://prwatech.in/blog/wp-content/uploads/2021/03/4-22.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/4output.jpg)
Scope of variable
![](https://prwatech.in/blog/wp-content/uploads/2021/03/11-12.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/11out.jpg)
String declaration
![](https://prwatech.in/blog/wp-content/uploads/2021/03/12-12.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/12out.jpg)
Operators
Using * operator [Multiply]
![](https://prwatech.in/blog/wp-content/uploads/2021/03/3-20.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/3output.jpg)
Logical AND operator [&&]
![](https://prwatech.in/blog/wp-content/uploads/2021/03/6-19-edited.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/6output.jpg)
Increment [++] and decrement [- -] operator
![](https://prwatech.in/blog/wp-content/uploads/2021/03/7-20.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/7out.jpg)
Logical operators
![](https://prwatech.in/blog/wp-content/uploads/2021/03/8-22.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/8output.jpg)
Shift operator
![](https://prwatech.in/blog/wp-content/uploads/2021/03/9-20.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/9out.jpg)
Bitwise Operators
![](https://prwatech.in/blog/wp-content/uploads/2021/03/10-12.jpg)
Output :
![](https://prwatech.in/blog/wp-content/uploads/2021/03/10out.jpg)