Go ControlFlow Flashcards
How many looping constructs are available in Go?
Only 1 - “For Loop”
What are different variations of For loop ?
- Loop till condition
- Loop till condition with “post clause”
- Loop over collections
- Infinite Loop
Is there any other “println” function apart from “fmt” package ?
Yes, the built-in “println” function.
What’s the main purpose of built-in “println” function ?
Debugging
How to declare “for-till-condition” loop ?
It only requires a “condition” after “for” keyword:
»>
var i int
for i < 5 {
println(i)
i++
}
»>
How to use “break” and “continue” statement with For loop ?
Used with “if-statement”:
»>
for i< 5 {
if i==3 {
continue //or break
}
}
How to declare “for-till-condition-with-post-clause” ?
> > > for i := 0 ; i < 5 ; i++ {
println(i)
}
How many semicolons are mandatory in “for-till-condition-with-post-clause” loop ?
Three
Can we declare the loop variable outside the for loop scope in “for-till-condition-with-post-caluse” loop ?
Yes, but we need to use the semicolon.
»>
var i int
for ; i<5 ; i++ {
println(i)
}
Declare infinite loop in Go-Lang ?
for {
println(“infiinte”)
}
Which keyword is required to iterate through any collection in Go ?
“range” keyword
What is returned by the “range” keyword ?
It returns:
1. index, value - in case of Arrays and slices
2. key, value - in case of Map
How to use for loop to iterate Array and Slices ?
> > > for i,v := range mySlice {
println(i,v)
}
Can we use only one value returned by “range” keyword instead of two ?
Yes, by ignoring the other value using “underscore”.
»>
for _ , v := range mySLice {
println(v)
}
»>
How to use “for loop” to iterate a Map ?
for k , v := range myMap {
println(k,v)
}
What is “panic” function ?
“panic” function, stops the executions of the program immediately and breaks all execution.
How to throw a “panic” ?
Just by calling the “panic” function:
»> panic (“something bad happend)
Create if-else-if statement ?
> > > if x > 5 {
println(“greater than 5)
} else if x == 5 {
println(“equal to 5”)
} else {
println(“smaller than 5”)
}
What’s a splice operation ?
Removing Item from a “slice” is called a splice operation.
How to remove an item from a slice ?
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:]…)
What is the meaning of ellipsis at the end of slice ?
It simply means maintain the order
How to delete an item from a slice while preserving the order ?
> > > updatedSlice := append(mySlice[:i], mySlice[i+1:]…)
Which function of “fmt” package is used to return an “error” with formatted string ?
fmt.Errorf(“User not found with id : ‘%v’”, id)
Write the format to create a switch-case structure in Go ?
> > > key := “first”
switch key {
case “first”:
fmt.Println(“first Case”)
case “second”:
fmt.Println(“second case”)
default:
fmt.Println(“deafult case”)
}
Is it mandatory to use “break” statement after each case ?
No, “break” statement is implicitly added to the block by compiler.
How to “fallthrough” case statements in switch case structure ?
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
}