Variables, DataTypes and Operators in GO (GoLang # 2)

Variables, DataTypes and Operators in GO (GoLang # 2)

ยท

5 min read

In the last blog of this series on Go programming Language, we looked into the basics of the language along with writing your first GO Program. in case you have missed it check it out here.

in this blog we will cover variables, constants, Data Types and operators in GoLang

Variables

Variables are user defined keywords to store particular values provided by the user, these values can change hence the name variables.

There are two ways to declare variables

  1. Implicit Declaration
  2. Explicit Declaration

we define variables using the syntax (Explicit Declaration)

var var_name type = value / expression

var - keyword to define a variable

var_name - what name you want to assign to your variable

type - datatype of the variable

value / expression - value you will assign to the variable

Constants

Constants can be defined using the syntax (Explicit Declaration)

const var_name type = value / expression

const - keyword to define a constant

var_name - name assigned to your constant

type - datatype of your constant

value / expression - value you will assign to the constant

implicit declaration

There is another way of declaring constants and variables and that is through the walrus operator (:=) in which you don't have to explicitly define the type of your variable / constant, this is also known as implicit declaration. go compiler will automatically assign a type to it just like in dynamically typed languages like python.

var a := 12
var b := 11.297 
var c := true


const a := 10
const b := 7.98
const c := false

variables and constants can have many different datatypes like integer, character, boolean, floating point(float) etc.

some of the most common datatypes you will use in Go are as follows

DataTypes

These specifies which type of value a variable / constant has and what type of mathematical, relational or logical operations can be applied to it without causing an error.

Integer

There are two types

  1. Signed
  2. Unsigned Integers

Each progressing type in Integer is used to store bigger and bigger values, this is also true for some of the other datatypes. it is mainly done for efficient use of memory.

Signed Integers

these are basically the Integer values ranging from -n to n.

8-bit signed Integer (int8)

it can have values from -128 to 127

var num_1 int8 = 120

var num_2 int8 = -111
16-bit signed Integer (int16)

it can have values from -32768 to 32767

var num_1 int16 = 12000

var num_2 int16 = -14560
32-bit signed Integer (int32)

it can have values from -2147483648 to 2147483647

var num_1 int32 = 27653278

var num_2 int32 = -7834578
64-bit signed Integer (int64)

it can have values from -9223372036854775808 to 9223372036854775807

var num_1 int64 = 87546785764356

var num_2 int64 = -3546798567487

Unsigned Integers

These are the values ranging from 0 to n.

8-bit unsigned Integer (uint8)

it can have values from 0 to 255

var num_1 uint8 = 120

var num_2 uint8 =  213
16-bit unsigned Integer (uint16)

it can have values from 0 to 65535

var num_1 uint16 = 34765

var num_2 uint16 =  12345
32-bit unsigned Integer (uint32)

it can have values from 0 to 4294967295

var num_1 uint32 = 54634526

var num_2 uint32 =  26734867
64-bit unsigned Integer (uint64)

it can have values from 0 to 18446744073709551615

var num_1 uint64 = 32566234677643

var num_2 uint64 =  20420030431

Float

Float or Floating points are basically decimal values and there are two types of float

  1. float32

  2. float64

float32

acquires 32 bits in memory

var num float32 = 123.43
float64

acquires 64 bits in memory

var num float64 = 356.563256

Strings

Strings are sequence of characters. It contains 8-bit bytes. By default, strings in Golang are UTF-8 encoded.

value assigned to string can be empty but not nil.

var name string = "Yash"

boolean

Boolean can only contain 2 values - True or False

a := 2
b := 4
a == b

Output - False

a != b

Output - True

Operators

There are many types of operators

  1. Arithmetic Operators
  2. Relational Operators
  3. logical Operators

Arithmetic Operators

These are used to perform arithmetic / mathematical operations

  • Addition ( + ) - Adds two or more numbers
  • Subtraction ( - ) - Subtracts two or more numbers
  • Multiplication ( * ) - Multiplies two or more numbers
  • Division ( / ) - Divides two or more numbers
  • Modulus ( % ) - gives the remainder
a := 5
b := 2

a + b
a - b
a * b
a / b
a % b

Output -

7

3

10

2.5

2

Relational Operators

Relational operators are used for the comparison of two value.

  • Equal to ( == )
  • NOT Equal to ( != )
  • Greater than ( > )
  • Less than ( < )
  • Greater than equal to ( >= )
  • Less than equal to ( <= )
a := 5
b := 3


a == b
a != b 
a > b
a < b 
a >= b
a <= b

Output -

false

true

true

false

true

false

Logical Operators

They are used to combine two or more conditions/constraints

  • Logical AND ( && ) - return true when both the conditions are true
  • Logical OR ( || ) - return true when either of the conditions is true
  • Logical NOT ( ! ) - returns true if condition is not satisfied
a := 5
b := 3

a > b && b <= 3

a != b || b < 7

! ( a == b )

Output -

true

true

true

There are a few more types of operators like assignment operator and bitwise operators, more on them in the later parts of this series.

as always, Thank You for reading this blog and I hope you liked it

See you in the next one.

Did you find this article valuable?

Support Yash Dhasmana by becoming a sponsor. Any amount is appreciated!

ย