Skip to content

Instantly share code, notes, and snippets.

@kobayashi
Last active October 23, 2016 15:20
Show Gist options
  • Save kobayashi/38b81d5c3fda0202104e60eaf456c6a7 to your computer and use it in GitHub Desktop.
Save kobayashi/38b81d5c3fda0202104e60eaf456c6a7 to your computer and use it in GitHub Desktop.
understand underscore
package main
import “fmt”
func main() {
s := []int{10, 20, 30, 40, 50, 60, 70, 80, 90}
fmt.Println(“--------\nindexWithValue\n--------“)
indexWithValue(s)
fmt.Println(“--------\nindexOnly\n--------“)
indexOnly(s)
fmt.Println(“--------\nvalueOnly\n--------“)
valueOnly(s)
fmt.Println(“--------\nnoValue\n--------“)
noValue(s)
}
func indexWithValue(slice []int) {
for index, value := range slice {
fmt.Println(index, value)
}
}
func indexOnly(slice []int) {
for index := range slice {
fmt.Println(index)
}
}
func valueOnly(slice []int) {
for _, value := range slice {
fmt.Println(value)
}
}
func noValue(slice []int) {
for range slice {
fmt.Println(“foo”)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment