3 ms·
stupid question, but from your second example what does the _ do? in this line? "for _, "
by swalsh 13y ago
stupid question, but from your second example what does the _ do? in this line?
"for _, "
- nuttendorfer 13y agoAFAIK the function returns to values but you only need name in this case, so _ discards that return value.
- troym 13y agoIt's the "blank identifier", a throw-away variable. See http://golang.org/ref/spec#Blank_identifier http://golang.org/ref/spec#Blank_identifier
- ominous_prime 13y ago"_" is when you must assign a value, but don't want to use it. range in the for loop return 2 values (index, value). If you don't want the index, you can assign it to "_" to ignore it. http://golang.org/ref/spec#Blank_identifier http://golang.org/ref/spec#Blank_identifier
- swalsh 13y agooh! that's pretty cool.
- steveklabnik 13y agoCheck your favorite language, it probably has this feature. Ruby, Haskell, tons of them.
- justinhj 13y agoFor iterates over a range and returns the index and the value. If you don't use the index the underscore just ignores it.
- deleted 13y ago[deleted]