Go ControlFlow Flashcards

1
Q

How many looping constructs are available in Go?

A

Only 1 - “For Loop”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are different variations of For loop ?

A
  1. Loop till condition
  2. Loop till condition with “post clause”
  3. Loop over collections
  4. Infinite Loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is there any other “println” function apart from “fmt” package ?

A

Yes, the built-in “println” function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the main purpose of built-in “println” function ?

A

Debugging

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to declare “for-till-condition” loop ?

A

It only requires a “condition” after “for” keyword:
»>
var i int
for i < 5 {
println(i)
i++
}
»>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to use “break” and “continue” statement with For loop ?

A

Used with “if-statement”:
»>
for i< 5 {
if i==3 {
continue //or break
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to declare “for-till-condition-with-post-clause” ?

A

> > > for i := 0 ; i < 5 ; i++ {
println(i)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many semicolons are mandatory in “for-till-condition-with-post-clause” loop ?

A

Three

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can we declare the loop variable outside the for loop scope in “for-till-condition-with-post-caluse” loop ?

A

Yes, but we need to use the semicolon.
»>
var i int
for ; i<5 ; i++ {
println(i)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Declare infinite loop in Go-Lang ?

A

for {
println(“infiinte”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which keyword is required to iterate through any collection in Go ?

A

“range” keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is returned by the “range” keyword ?

A

It returns:
1. index, value - in case of Arrays and slices
2. key, value - in case of Map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to use for loop to iterate Array and Slices ?

A

> > > for i,v := range mySlice {
println(i,v)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can we use only one value returned by “range” keyword instead of two ?

A

Yes, by ignoring the other value using “underscore”.
»>
for _ , v := range mySLice {
println(v)
}
»>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to use “for loop” to iterate a Map ?

A

for k , v := range myMap {
println(k,v)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is “panic” function ?

A

“panic” function, stops the executions of the program immediately and breaks all execution.

16
Q

How to throw a “panic” ?

A

Just by calling the “panic” function:
»> panic (“something bad happend)

17
Q

Create if-else-if statement ?

A

> > > if x > 5 {
println(“greater than 5)
} else if x == 5 {
println(“equal to 5”)
} else {
println(“smaller than 5”)
}

18
Q

What’s a splice operation ?

A

Removing Item from a “slice” is called a splice operation.

19
Q

How to remove an item from a slice ?

A

It can be done by following steps:
1. Take the slice upto the element (but not including the element)
2. Take the slick from next element till the end of the slick
3. Append both slice
»>
finalSlice := append( mySlice[:i], mySlice[i+1:]…)

20
Q

What is the meaning of ellipsis at the end of slice ?

A

It simply means maintain the order

21
Q

How to delete an item from a slice while preserving the order ?

A

> > > updatedSlice := append(mySlice[:i], mySlice[i+1:]…)

22
Q

Which function of “fmt” package is used to return an “error” with formatted string ?

A

fmt.Errorf(“User not found with id : ‘%v’”, id)

23
Q

Write the format to create a switch-case structure in Go ?

A

> > > key := “first”
switch key {
case “first”:
fmt.Println(“first Case”)
case “second”:
fmt.Println(“second case”)
default:
fmt.Println(“deafult case”)
}

24
Q

Is it mandatory to use “break” statement after each case ?

A

No, “break” statement is implicitly added to the block by compiler.

25
Q

How to “fallthrough” case statements in switch case structure ?

A

By default, Go adds the break statement at the end of every case statement. However, if user wants to “fallthrough” the “case” statement, he/she can use the keyword : “fallthrought”:
»>
switch a {
case “first”:
fmt.Println(“first”)
fallthrough
case “second”
fmt.Println(“second”)
fallthrough
}