for loop in golang code example
Example 1: go for loop
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
Example 2: golang loop
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
Example 3: go for loop
sum := 0
for i := 1; i < 5; i++ {
sum += i
}
fmt.Println(sum)
Example 4: range in go
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
Example 5: go loops
for i := 1; i < 10; i++ {
}
for ; i < 10; {
}
for i < 10 {
}
for {
}
here:
for i := 0; i < 2; i++ {
for j := i + 1; j < 3; j++ {
if i == 0 {
continue here
}
fmt.Println(j)
if j == 2 {
break
}
}
}
there:
for i := 0; i < 2; i++ {
for j := i + 1; j < 3; j++ {
if j == 1 {
continue
}
fmt.Println(j)
if j == 2 {
break there
}
}
}
Example 6: golang for
for i, s := range arr {
}
for k,v := range map{
}
for{
break
}