Go Mod 2
Go Mod 2
package main
import ("fmt")
func main() {
x:= 20
y:= 18
if x > y {
fmt.Println("x is greater than y")
}
}
IF ELSE:
package main
import ("fmt")
func main() {
time := 20
if (time < 18) {
fmt.Println("Good day.")
} else {
fmt.Println("Good evening.")
}
}
package main
import ("fmt")
func main() {
day := 4
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
case 6:
fmt.Println("Saturday")
case 7:
fmt.Println("Sunday")
}
}
Go for Loop
Loops are handy if you want to run the same code over and over again, each
time with a different value.
EXAMPLE:
package main
import ("fmt")
func main() {
fmt.Println(i)
}
BREAK STATEMENT
package main
import "fmt"
func main() {
if i == 5 {
}
}
fmt.Println("Exiting program")
OUTPUT:
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
Exiting program
CONTINUE STATEMENT:
package main
import "fmt"
func main() {
if i == 5 {
fmt.Println("Continuing loop")
}
}
fmt.Println("Exiting program")
OUTPUT:
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
Continuing loop
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9
Exiting program
FUNCTIONS :
OUTPUT:
package main
import ("fmt")
// Create a function
func myMessage() {
func main() {
RECURSIVE FUNCTIONS:
package main
import ("fmt")
if x == 11 {
return 0
fmt.Println(x)
return testcount(x + 1)
func main(){
testcount(1)
}
Defer in GO:
https://www.geeksforgeeks.org/defer-keyword-in-golang/
(in functions)
In Golang, a function that can be called with a variable argument list is known
as a variadic function. One can pass zero or more arguments in the variadic
function. If the last parameter of a function definition is prefixed by ellipsis …,
then the function can accept any number of arguments for that parameter.
Here ... operator tells Golang program to store all arguments of Type in elem
parameter.
package main
import (
"fmt"
sum := 0
sum += j
}
return sum
func main() {
Output:
Sum = 45
GO Closures:
https://www.programiz.com/golang/closure