Is there a foreach loop in Go for iterating over slice elements?
Hello everyone! I just started learning Go after Python and JavaScript, and I encountered an unexpected difficulty. In Python I'm used to writing `for item in my_list:`, in JS — `arr.forEach(item => ...)`. But in Go I only see the classic `for i := 0; i < len(slice); i++ {}` — is this normal?
I have a simple slice of strings:
```go
names := []string{"Alice", "Bob", "Charlie"}
```
I want to print each name on a separate line. In Python I would write:
```python
for name in names:
print(name)
```
But in Go I have to write like this:
```go
for i := 0; i < len(names); i++ {
fmt.Println(names[i])
}
```
This works, but looks cumbersome. I feel like I'm missing some elegant syntax. I tried googling "foreach in Go", but the answers confuse me — some say there is no fo
No answers yet.
Log in to leave an answer
Blogs