Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Go Operators
3.
Arithmetic Operators
3.1.
Example
4.
Assignment Operators
4.1.
Example
5.
Relational Operators
5.1.
Example
6.
Logical Operators
6.1.
Example
7.
Bitwise Operators
7.1.
Example
8.
Miscellaneous Operators
8.1.
Example
9.
Go Data Types
10.
Boolean 
11.
Numeric 
12.
String
13.
Derived types
14.
Integer Data Type
15.
Floating Types
16.
Other Numeric Types
17.
FAQs
18.
Key Takeaways
Last Updated: Mar 27, 2024

Datatypes & Operators

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Operators are used to performing an operation on variables and values, and data types are checked with an in-depth system used for putting forward variables or functions of various types. In this blog, we discuss all types of data types and operators.

Go Operators

Operators are the foundation of any language. Hence the functionality of the go language is incomplete without using operators. Operators permit us to perform one-of-a-kind types of operations on operands. Ingo language, operators may be classified based upon their different functionality:

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Arithmetic Operators

Arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication, etc.

  • Addition: The '+' operator adds two operands—for example, x+y.
  • Subtraction: The '-' operator subtracts two operands—for example, x-y.
  • Multiplication: The '*' operator multiplies two operands—for example, a*b.
  • Division: Here '/' operator divides the first operand by the second—for example, x/y.
  • Modulus: The '%' operator returns the remainder value when the first operand is divided by the second—for example, x%y.

Example

package main
import "fmt"
func main() 
{
	var x, y = 35, 7
	fmt.Printf("x + y = %d\n", x+y)
	fmt.Printf("x - y = %d\n", x-y)
	fmt.Printf("x * y = %d\n", x*y)
	fmt.Printf("x / y = %d\n", x/y)
	fmt.Printf("x mod y = %d\n", x%y)
	x++
	fmt.Printf("x++ = %d\n", x)
	y--
	fmt.Printf("y-- = %d\n", y)
}

Output

x + y = 42
x - y = 28
x * y = 245
x / y = 5
x mod y = 0
x++ = 36
y-- = 6

Assignment Operators

  • "=" (Simple Assignment): This is the most straightforward assignment operator. This operator is used to assign all values at the proper to the variable at the left.
  • "+=" (Add Assignment): This operator is the combination of '+' and '=' operators. This operator first adon the left present-day value of the variable at the left to the value at the proper and then assigns the result to the variable on the left.
  • "-=" (Subtract Assignment): This operator first subtracts the present-day value of the variable on the left from the value at the proper and then assigns the result to the variable on the left.
  • "*=" (Multiply Assignment): This operator is the mixture of '*' and '=' operators. This operator first multiplies the given value of the variable at the left to the value on the right, and after that, it assigns the result to the variable on the left.
  • "/=" (Division Assignment): This operator is the combination of '/' and '=' operators. This operator first divides the given value of the variable on the left by the value at the right and then assigns the end result to the variable at the left.
  • "%=" (Modulus Assignment): This operator is the mixture of '%' and '=' operators. This operator first modulo the given value of the variable on the left by way of the value at the right and then assigns the end result to the variable on the left.
  • "&=" (Bitwise AND Assignment): This operator is the mixture of '&' and '=' operators. This operator first "Bitwise AND" the given value of the variable on the left by the cost at the right and then assigns the result to the variable at the left.
  • "^=" (Bitwise Exclusive OR): This operator is the mixture of '^' and '=' operators. This operator first "Bitwise exclusive OR" the given value of the variable on the left via the price at the right and then assigns the end result to the variable at the left.
  • "|=" (Bitwise Inclusive OR): This operator is the mixture of '|' and '=' operators. This operator first "Bitwise Inclusive OR" the given value of the variable on the left via the fee at the right, after which it assigns the result to the variable at the left.

Example

package main
import "fmt"
func main() {
	var x, y = 15, 25
	x = y
	fmt.Println("= ", x)
	x = 15
	x += y
	fmt.Println("+=", x)
	x = 50
	x -= y
	fmt.Println("-=", x)
	x = 2
	x *= y
	fmt.Println("*=", x)
	x = 100
	x /= y
	fmt.Println("/=", x)
	x = 40
	x %= y
	fmt.Println("%=", x)
}

Output

= 25
+= 40
-= 25
*= 50
/= 4
%= 15

Relational Operators

This operator is mainly used to compare two values. Let's see them one by one:

  • '!=' (Not Equal To) The operator compares the two operands to see if they are equal. It returns true when the condition is not met. It returns false otherwise. The '==' operator has an exact boolean complement. 5!=5 returns false, for example.
  • '==' (Equal To) operator checks when the two given operands are equal or not. It returns true. Otherwise, it returns false. For example, 5==5 will return true.
  • The '>' (Greater Than)operator checks whether the first operand is greater than the second. It returns true. Otherwise, it returns the false. For example, 6>5 will return true.
  • '< '(Less Than)operator checks whether the first operand is lesser than the second. If so, it returns true. Otherwise, it returns false. For example, 6<5 will return false.
  • '>=' (Greater Than Equal To)The operator compares the first and second operands to see if the first operands are larger than or equal to the second operands. When this is the case, it returns true. Otherwise, false is returned. 5>=5 will, for example, return true.
  • '<=' (Less Than Equal To)operator checks whether the first operand is lesser than or equal to the second operand. It returns true. Otherwise, it returns false. For example, 5<=5 will also return true.

Example

package main
import "fmt"
func main() {
	var x, y = 15, 25
	fmt.Println(x == y)
	fmt.Println(x != y)
	fmt.Println(x < y)
	fmt.Println(x <= y)
	fmt.Println(x > y)
	fmt.Println(x >= y)
}

Output

false
true
true
true
false
False

Logical Operators

They combine two or more conditions/constraints or complement the evaluation of the original condition in consideration.  

  • Logical AND: When both of the conditions in question are true, the && operator returns true; otherwise, it returns false. When a and b are true, for example, a && b returns true.(i.e., non-zero).
  • Logical OR: The || operator returns true when one (or both) of the situations in consideration is satisfied; else, it returns false. For example, a || b returns true when one of a or b is true (i.e., non-zero). Of course, it returns true when both a and b are true.
  • Logical NOT: The ! operator returns true the condition in consideration is not satisfied; else, it returns false. For example, !a returns true when a is false, i.e., when a=0.

Example

package main
import "fmt"
func main() {
	var x, y, z = 10, 20, 30
	fmt.Println(x < y && x > z)
	fmt.Println(x < y || x > z)
	fmt.Println(!(x == y && x > z))
}

Output

false
true
true

Bitwise Operators

In Go language, six bitwise operators work at bit level or perform bit by bit operations. Following are the bitwise operators: 

  • &: When we take two numbers as operands and do AND on every bit of two numbers. The result of AND is 1 if both bits are 1.
  • |: When we take two numbers as operands and do OR on each bit of two numbers. The result of OR is 1 when any of the two bits is 1.
  • ^: When we take two numbers as operands and do XOR on every bit of two numbers. The result of XOR is 1 if two bits are different.
  • <<: When we take two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
  • >>: When we take two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
  • &^: This is a bit clear operator.

Example

package main
import "fmt"
func main() {
	var x uint = 9 //0000 1001
	var y uint = 65 //0100 0001
	var z uint
	z = x & y
	fmt.Println("x & y =", z)
	z = x | y
	fmt.Println("x | y =", z)
	z = x ^ y
	fmt.Println("x ^ y =", z)
	z = x << 1
	fmt.Println("x << 1 =", z)
	z = x >> 1
	fmt.Println("x >> 1 =", z)
}

Output

x & y = 1
x | y = 73
x ^ y = 72
x << 1 = 18
x >> 1 = 4

 

Read about Bitwise Operators in C here.

Miscellaneous Operators

  • *: This operator provides a pointer to the variable.
  • <-: The name of this operator is received, which is used to receive a value from the channel.
  • &: This operator returns the address of a variable.

Example

package main  
import "fmt"  
func main() {
  a := 4   
  // Using address of operator(&) and
  // pointer indirection(*) operator
  b := &a
  fmt.Println(*b)
  *b = 7
  fmt.Println(a)
}

Output: 

4
7

Go Data Types

In Go programming, data types refer to a wide framework for declaring various types of variables and functions. A variable's type determines how much storage space it requires and how the stored bit pattern is interpreted.

Boolean 

They are boolean types and consist of the two predefined constants: (a) true (b) false

Numeric 

They are arithmetic types representing a) integer types or b) floating-point values throughout the program.

String

A string type is a collection of string values. Its value is a byte sequence. Strings are immutable types that are once created. It's not possible to change the contents of a string. The predeclared string type is a string.

Derived types

They include (a) Array types, (b) Pointer types, (c) Structure types, (d) Function types, and (e) Union types, f) Slice types g) Interface types h) Map types i) Channel Types

Let's discuss the mainly used data types in detail.

Integer Data Type

The predefined architecture-independent integer types are −

uint8- Unsigned 8-bit integers (0 to 255)

 

uint16- Unsigned 16-bit integers (0 to 65535)

 

uint32- Unsigned 32-bit integers (0 to 4294967295)

 

uint64- Unsigned 64-bit integers (0 to 18446744073709551615)

 

int8- Signed 8-bit integers (-128 to 127)

 

int16- Signed 16-bit integers (-32768 to 32767)

 

int32- Signed 32-bit integers (-2147483648 to 2147483647)

 

int64- Signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

Floating Types

The predefined architecture-independent float types are −

 

float32- IEEE-754 32-bit floating-point numbers

float64- IEEE-754 64-bit floating-point numbers

complex64- Complex numbers with float32 real and imaginary parts

complex128- Complex numbers with float64 real and imaginary parts

Other Numeric Types

There is a set of numeric types with implementation-specific sizes −

byte- Unsigned 8-bit integers (0 to 255)

rune- same as int32

uint- 32 or 64 bits

int- 32 or 64 bits

uintptr- an unsigned integer to store the uninterpreted bits of a pointer value.

FAQs

  1. What is the size of an int data type?
    Ans: The size of the data types depends on the system.
     
  2. What is the use of the assignment operator?
    Ans: We use the assignment operator to assign values to a variable. 
     
  3. What is Boolean Data Type?
    Ans: The boolean data type has two possible values, either true or false.

Key Takeaways

This article extensively discussed Datatypes & Operators topics and all the types in this article.

Here, you ought to have a better understanding of some of the most common data types available in Go. As you work on Go programming projects, these data types will become increasingly significant.

To study more about data types, refer to Abstract Data Types in C++.

"We hope that this blog enhances your knowledge regarding Datatypes & Operators, and if you would like to learn more about this, you can view *Data Types in C* . Do upvote our blog to help other ninjas grow. Happy Coding!"

Live masterclass