Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Created January 24, 2022 11:40
Show Gist options
  • Save manjeettahkur/b4049a3d619e952fe0b2cd320708aaa8 to your computer and use it in GitHub Desktop.
Save manjeettahkur/b4049a3d619e952fe0b2cd320708aaa8 to your computer and use it in GitHub Desktop.
Why this code return this {5,5,5,5,5} ?
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