Skip to content

Instantly share code, notes, and snippets.

@nileshsimaria
Last active July 18, 2019 03:19
Show Gist options
  • Save nileshsimaria/44011fda746743d0a5a8e73d138b0718 to your computer and use it in GitHub Desktop.
Save nileshsimaria/44011fda746743d0a5a8e73d138b0718 to your computer and use it in GitHub Desktop.
golang: for + range
package main
import "fmt"
func main() {
a := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
b := make([]*int, 10)
for i, v := range a {
b[i] = &v
}
for _, v := range b {
fmt.Printf("%d ", *v)
}
fmt.Println()
}
/*
* This program is priting 9 9 9 9 9 9 9 9 9 9
* Why ?
*
* Hint:
* Think of iteration variable 'v' of line 10
* and short variable declaration
*
* Read language specification of for + range
* https://golang.org/ref/spec?source=post_page---------------------------#For_range
*
* Playground link: https://play.golang.org/p/id54Wi7ISp-
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment