Created
January 24, 2022 11:40
-
-
Save manjeettahkur/b4049a3d619e952fe0b2cd320708aaa8 to your computer and use it in GitHub Desktop.
Why this code return this {5,5,5,5,5} ?
This file contains hidden or 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() { | |
first := []string{"1","2","3","4","5"}; | |
second := []*string{} | |
for _, item := range first { | |
second = append(second, &item) | |
} | |
for _, item := range second { | |
fmt.Println(*item) | |
} | |
} | |
// What will be the output of this programmer | |
//Fix | |
for index := range first { | |
second = append(second, &first[index]) | |
} | |
// Why this programme behave like this | |
package main | |
import "fmt" | |
func main() { | |
first := []string{"1", "2", "3", "4", "5"} | |
second := []*string{} | |
var item string | |
for i := 0; i < len(first); i++ { | |
item = first[i] | |
second = append(second, &item) | |
} | |
for _, item := range second { | |
fmt.Println(*item) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment