0% found this document useful (0 votes)
13 views8 pages

Go Mod 2

The document provides an overview of control flow constructs in Go, including if-else statements, switch statements, for loops, break and continue statements, and functions. It explains how to define and use these constructs with examples, including recursive functions and variadic functions that accept a variable number of parameters. Additionally, it briefly mentions defer and closures in Go, providing links for further reading.

Uploaded by

bagheera.gunnu18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Go Mod 2

The document provides an overview of control flow constructs in Go, including if-else statements, switch statements, for loops, break and continue statements, and functions. It explains how to define and use these constructs with examples, including recursive functions and variadic functions that accept a variable number of parameters. Additionally, it briefly mentions defer and closures in Go, providing links for further reading.

Uploaded by

bagheera.gunnu18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

If else construct:

Use the if statement to specify a block of Go code to be executed if a condition


is true.
Example:

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.")
}
}

The switch Statement


Use the switch statement to select one of many code blocks to be executed.

The switch statement in Go is similar to the ones in C, C++, Java, JavaScript,


and PHP. The difference is that it only runs the matched case so it does not need
a break statement.

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.

Each execution of a loop is called an iteration.

EXAMPLE:

package main

import ("fmt")

func main() {

for i:=0; i < 5; i++ {

fmt.Println(i)
}

BREAK STATEMENT

package main

import "fmt"

func main() {

​ for i := 0; i < 10; i++ {

​ ​ if i == 5 {

​ ​ ​ fmt.Println("Breaking out of loop")

​ ​ ​ break // break here

​ ​ }

​ ​ fmt.Println("The value of i is", i)

​ }

​ 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

Breaking out of loop

Exiting program

CONTINUE STATEMENT:

package main

import "fmt"

func main() {

​ for i := 0; i < 10; i++ {

​ ​ if i == 5 {

​ ​ ​ fmt.Println("Continuing loop")

​ ​ ​ continue // break here

​ ​ }

​ ​ fmt.Println("The value of i is", i)

​ }

​ 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 :

A function is a block of statements that can be used repeatedly in a program.

A function will not execute automatically when a page loads.

A function will be executed by a call to the function.

OUTPUT:

package main

import ("fmt")

// Create a function
func myMessage() {

fmt.Println("I just got executed!")

func main() {

myMessage() // call the function

RECURSIVE FUNCTIONS:

package main

import ("fmt")

func testcount(x int) int {

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/

Passing a variable number of parameters:

(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.

Syntax of a variadic function:


func f(elem ...Type)

Here ... operator tells Golang program to store all arguments of Type in elem
parameter.

// Go program that uses a function with variable argument list

package main

import (

​ "fmt"

// the sum of the numbers


func add(num ...int) int {

​ sum := 0

​ for j := range num {

​ ​ sum += j

​ }

​ return sum

func main() {

​ fmt.Println("Sum =", add(1, 2, 3, 4, 5, 7, 8, 6, 5, 4))

Output:
Sum = 45

GO Closures:

https://www.programiz.com/golang/closure

You might also like