To iterate over characters in a string in Go, you can use for loop with range over the string to get individual characters. During each iteration of the for loop, we can access the index, and character at that index in the string.
In this example,
str
with a value of "Hello"
.range
keyword to iterate over the string, which returns Unicode code points.string(char)
.package main
import "fmt"
func main() {
str := "Hello"
for _, char := range str {
fmt.Println(string(char))
}
}
H e l l o
In this tutorial, we learned how to iterate over characters in a string in Go language using for each loop with range, with well detailed examples.