use a basic loop in Go
Code snippet on how to use a basic loop in Go
package main
import "fmt"
func main() {
// define a number
num := 5
// loop from 0 to num
for i := 0; i < num; i++ {
fmt.Println(i)
}
}
This code sample demonstrates a basic loop using Go. It begins by importing the fmt package in order to use its Println function. Then a variable num is defined as 5. The loop starts with “for i := 0;” which sets the initial value of i to 0. The middle clause “i < num” is a condition that must be true in order for the loop to continue. This condition is checked before each iteration of the loop and prevents the loop from running after i reaches 5 (since it is less than num). Finally, the last clause of the loop “i++” increases the value of i by 1 after each iteration. The loop runs until i is equal to 5, then it stops. Inside the loop, the Println function prints the current value of i to the console. This loop runs 5 times, from 0 to 4, and prints each number to the console.