Numeric Data Types in Go
• Go provides several numeric data types, including integers,
floating-point numbers, and complex numbers.
• 1. Integer Types
• Go has both signed and unsigned integer types, available in
different sizes.
• int and uint: These are the most efficient integer types for a
given platform (either 32-bit or 64-bit).
• uintptr: A special unsigned integer type that can hold the
value of a pointer.
2. Rune and Byte
•rune: An alias for int32, used to represent Unicode characters.
•byte: An alias for uint8, used to represent raw binary data.
3. Signed and Unsigned Integer Ranges
• Signed integers use two’s complement representation,
meaning the range is from −2ⁿ⁻¹ to 2ⁿ⁻¹−1.
• Unsigned integers use all bits for positive values,
so the range is from 0 to 2ⁿ−1.
Example:
•int8 range: −128 to 127
•uint8 range: 0 to 255
Operators in go lang
• Arithmetic Operators
• These are used to perform arithmetic/mathematical
operations on operands in Go language:
• 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, x*y.
• Division: The ‘/’ operator divides the first operand by the
second. For example, x/y.
• Modulus: The ‘%’ operator returns the remainder when
the first operand is divided by the second. For example,
x%y .
• package main
• import "fmt"
// Multiplication
• func main() {
result3:= p * q
• p:= 34
[Link]("\nResult of p * q = %d", result3)
• q:= 20
// Division
• // Addition
result4:= p / q
• result1:= p + q
[Link]("\nResult of p / q = %d", result4)
• [Link]("Result of p + q = %d", result1)
// Modulus
• // Subtraction
result5:= p % q
• result2:= p - q
[Link]("\nResult of p %% q = %d", result5)
• [Link]("\nResult of p - q = %d",
}
result2)
Relational Operators
• ‘=='(Equal To) operator checks whether the two
given operands are equal or not. If so, it returns true.
Otherwise, it returns false
• ‘!='(Not Equal To) operator checks whether the two
given operands are equal or not. If not, it returns true.
• ‘>'(Greater Than)operator checks whether the first
operand is greater than the second operand. If so, it
returns true.
• ‘<‘(Less Than)operator checks whether the first
operand is lesser than the second operand
• ‘>='(Greater Than Equal To)operator checks
whether the first operand is greater than or equal to
the second operand.
• ‘<='(Less Than Equal To)operator checks
whether the first operand is lesser than or equal to
the second operand.
• package main • // ‘<‘(Less Than)
• import "fmt" • result3:= p < q
• func main() { • [Link](result3)
• p:= 34 • // ‘>'(Greater Than)
• q:= 20 • result4:= p > q
• // ‘=='(Equal To) • [Link](result4)
• result1:= p == q • // ‘>='(Greater Than Equal To)
• [Link](result1) • result5:= p >= q
• // ‘!='(Not Equal To) • [Link](result5)
• result2:= p != q • // ‘<='(Less Than Equal To)
• [Link](result2) • result6:= p <= q
•
• [Link](result6) }
Logical operators
• Logical AND: The ‘&&’ operator returns true
when both the conditions in consideration are
satisfied
• Logical OR: The ‘||’ operator returns true
when one (or both) of the conditions in
consideration is satisfied. Otherwise it returns
false
• Logical NOT: The ‘!’ operator returns true the
condition in consideration is not satisfied.
Otherwise it returns false.
package main
import "fmt"
func main() {
var p int = 23
var q int = 60
if(p!=q && p<=q){
[Link]("True")
}
if(p!=q || p<=q){
[Link]("True")
}
if(!(p==q)){
[Link]("True")
}
}
• &: This operator returns the address of the
variable.
• *: This operator provides pointer to a variable.
• package main
•
• import "fmt"
•
• func main() {
• a := 4
•
• // Using address of operator(&) and
• // pointer indirection(*) operator
• b := &a
• [Link](*b)
• *b = 7
• [Link](a)
• }
Floating-Point Numbers:
In Go language, floating-point numbers are
divided into two categories as shown in the
below table.
package main
import "fmt"
func main() {
var x float32 = 5.00
var y float32 = 2.25
//Addition
[Link]("addition : %g + %g = %g\n ", x, y, x+y)
//Subtraction
[Link]("subtraction : %g - %g = %g\n", x, y, x-y)
//Multiplication
[Link]("multiplication : %g * %g = %g\n", x, y, x*y)
//Division
[Link]("division : %g / %g = %g\n", x, y, x/y)
}
Complex Numbers:
• The in-built function creates a complex number from
its imaginary and real part and in-built imaginary and
real function extract those parts.
• There are few built-in functions in complex numbers:
• complex – make complex numbers from two floats.
• real() – get real part of the input complex number as a float
number.
• imag() – get imaginary of the input complex number part as
float number
Booleans
• The boolean data type represents only one bit of information either true or false.
• The values of type boolean are not converted implicitly or explicitly to any
other type.
• package main
• import "fmt"
• func main() {
• var isGolangFun bool = true
• var isSkyGreen bool = false
• [Link]("Is Golang fun?", isGolangFun)
• [Link]("Is the sky green?", isSkyGreen)
• // Boolean operations
• [Link]("true AND false:", true && false)
• [Link]("true OR false:", true || false)
• [Link]("NOT true:", !true)
• }
A string in Go is a sequence of characters (Unicode code points).
Strings are immutable, meaning once created, they cannot be
changed.
package main
import "fmt"
func main() {
str1 := "Hello"
str2 := "Go"
// String concatenation
result := str1 + " " + str2
[Link](result) // Output: Hello Go
}
String Operations in Go
1. String Concatenation (+ Operator)
package main
import "fmt"
func main() {
str1 := "Hello"
str2 := "Go"
result := str1 + " " + str2
[Link](result) // Output: Hello Go
}
2 Finding String Length (len())
[Link](len("Golang")) // Output: 6
3. Accessing Characters
(Indexing)
str := "Hello"
[Link](string(str[0])) // Output: H
4. String Iteration (Using for Loop)
str := "GoLang"
for i, char := range str {
[Link]("Index: %d, Character:
%c\n", i, char)
}
5 String Comparison (== and
!=)
[Link]("Go" == "Go") // true
[Link]("Go" != "Java") // true
6. Substring Check ([Link]())
import "strings"
[Link]([Link]("Golang", "Go")) // true
7. String Splitting ([Link]())
words := [Link]("Hello Go World", " ")
[Link](words) // Output: [Hello Go World]
Control statements
• If statement
• It is used to decide whether a certain statement
or block of statements will be executed or not.
• if condition {
// statements to execute
}
package main
import "fmt"
func main() {
// taking a local variable
var v int = 700
// using if statement for
// checking the condition
if v < 1000 {
// print the following if
// condition evaluates to true
[Link]("v is less than 1000\n")
}
[Link]("Value of v is : %d\n", v)
}
If else condition
• If condition {
• // executes this block if
• //condition is true
} else {
//execute if condition is false
}
package main
import "fmt"
func main() {
// taking a local variable
var v int = 1200
// using if statement for
// checking the condition
if v < 1000 {
// print the following if
// condition evaluates to true
[Link]("v is less than 1000\n")
} else {
// print the following if
// condition evaluates to true
[Link]("v is greater than 1000\n")
}
Nested if Statement
• if condition1 {
• // Executes when condition1 is true
•
• if condition2 {
• // Executes when condition2 is true
• }
• }
package main
import "fmt"
func main() {
// taking two local variable
var v1 int = 400
var v2 int = 700
// using if statement
if( v1 == 400 ) {
// if condition is true then
// check the following
if( v2 == 700 ) {
// if condition is true
// then display the following
[Link]("Value of v1 is 400 and v2 is 700\n" );
}
}
• Go language contains only a single loop that is
for-loop.
• A for loop is a repetition control structure that allows
us to write a loop that is executed a specific number
of times.
• for initialization; condition; increment/decrement {
• // Code block
•}
• Different Ways to Use for Loop
• Standard for loop (like in C/C++/Java)for loop
• as a while loop (condition only)
• Infinite for loop (without condition)
• for loop with range (used for iterating over
arrays, slices, maps, etc.)
What is Scope?
• Scope determines where a declared name
(variable, function, etc.) is visible in the
program.
• It is a compile-time property.
package main
import "fmt"
func main() {
for i := 0 ; i < 4; i++{
[Link](“shivangi\n")
}
}
Types of Scope in Go
• 1. Universe Scope: Built-in types, functions,
constants (e.g., int, true, len).
• 2. Package Scope: Declarations outside any
function, visible within the package.
• 3. File Scope: Imported packages are visible only
within the file.
• 4. Function Scope: Variables declared inside a
function are local to it.
• 5. Block Scope: Variables declared within a block
(e.g., loops, if statements) are limited to that
block.
• package main
• import "fmt"
• // Package Scope: visible to the entire package
• var packageVar = "I am at package scope"
• func main() {
• // Function Scope: visible only within main()
• var functionVar = "I am at function scope"
•
• if true {
• // Block Scope: visible only inside this block
• blockVar := "I am at block scope"
• [Link](blockVar) // Accessible here
• }
• // [Link](blockVar) // Error: blockVar is not defined outside the block
• [Link](packageVar) // Accessible here
• [Link](functionVar) // Accessible here
• }
• func anotherFunction() {
• [Link](packageVar) // Accessible here
• // [Link](functionVar) // Error: functionVar is not accessible here
• }
Lexical Blocks and Shadowing
• - A lexical block is a grouping of statements
determining scope.
• - The compiler searches for a variable
declaration from the innermost to outermost
blocks.
• - Shadowing occurs when an inner declaration
hides an outer one.
•
•A lexical block is a scope where identifiers remain valid.
•Go follows lexical scoping, meaning inner blocks can
access outer variables.
•Variables declared inside functions, loops, and
conditionals are not accessible outside their blocks.
•Closures in Go retain access to variables from their
enclosing lexical scope.