Last active
July 18, 2019 03:19
-
-
Save nileshsimaria/44011fda746743d0a5a8e73d138b0718 to your computer and use it in GitHub Desktop.
golang: for + range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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