Re: Draft RFC: foreach iteration of keys without values
| From: | tyson andre | Date: | Wed, 02 Sep 2020 23:45:00 +0000 |
| Subject: | Re: Draft RFC: foreach iteration of keys without values | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111799@lists.php.net to get a copy of this message | ||
Hi Benas IML,
> I'd also like to mention that other languages don't have any kind of
> syntactic sugar/special syntax for iterating over keys. That is achieved
> using a function though.
That's not true, Golang has iterating over keys by default and a more verbose syntax to iterate
over values.
https://gobyexample.com/range
_ is used to indicate to the compiler that the result should be discarded..
```
// range on map iterates over key/value pairs.
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
// iterate over just the keys of a map.
for k := range kvs {
fmt.Println("key:", k)
}
// iterate over just the values of a map.
for _, v := range kvs {
fmt.Println("key:", k)
}
```
Cheers,
- Tyson