1. What is the basic loop construct in Go?
Answer:
Explanation:
Go uses the 'for' keyword as its basic and only loop construct.
2. How do you create an infinite loop in Go?
Answer:
Explanation:
In Go, an infinite loop can be created using 'for {}' or 'for true {}'.
3. How do you exit a loop in Go?
Answer:
Explanation:
The 'break' statement is used to exit a loop in Go.
4. What is the purpose of the 'continue' statement in Go loops?
Answer:
Explanation:
The 'continue' statement in Go skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
5. How do you write a for loop in Go that iterates from 0 to 9?
Answer:
Explanation:
In Go, a for loop that iterates from 0 to 9 is written as 'for i := 0; i < 10; i++ {}'.
6. Can you declare multiple variables in the initialization statement of a Go for loop?
Answer:
Explanation:
Go allows the declaration of multiple variables in the initialization statement of a for loop.
7. How do you iterate over an array or slice in Go using a loop?
Answer:
Explanation:
Iteration over an array or slice in Go can be done using a traditional for loop with an index or using the range keyword with a for loop.
8. What is the use of the range keyword in a Go loop?
Answer:
Explanation:
The range keyword in Go is used to iterate over elements of data structures like arrays, slices, maps, and channels.
9. How do you write a loop in Go that continues as long as a condition is true?
Answer:
Explanation:
In Go, a loop that continues as long as a condition is true is written using the for keyword, e.g., 'for condition {}'.
10. Is it possible to omit all three components (initialization, condition, post) of a Go for loop?
Answer:
Explanation:
In Go, it's possible to omit all three components of the for loop, resulting in an infinite loop.
11. Can a for loop in Go have multiple post statements?
Answer:
Explanation:
In Go, a for loop can have multiple post statements, separated by commas.
12. How do you nest loops in Go?
Answer:
Explanation:
Nested loops in Go are created by writing one loop inside another loop.
13. What happens if you omit the loop condition in a Go for loop?
Answer:
Explanation:
Omitting the loop condition in a Go for loop turns it into an infinite loop.
14. Can the range keyword in a Go loop return the index or key only?
Answer:
Explanation:
When using the range keyword in a Go loop, you can choose to get the index or key only by omitting the value variable.
15. In a Go for loop, is it mandatory to use the post statement?
Answer:
Explanation:
The post statement in a Go for loop is not mandatory. You can omit it if not needed for the logic of your loop.