I am trying to write the equivalent of the following line in python
H = [e for e in G if condition(e)]
Here is my example code. Basically I am just trying to use a getter function (G.get) to get a subset of G
. So I think I want arr2
to be a new array but containing the same object.
package main
import "fmt"
type Object struct {
x int
}
type Group []Object
func (G *Group) get() (H []Object) {
for _,v := range *G {
H = append(H,v)
}
return
}
func main() {
arr := make(Group,1)
arr[0].x = 1
fmt.Println(arr)
arr2 := arr.get()
arr[0].x = 3
fmt.Println(arr)
fmt.Println(arr2)
}
Which compiles and runs and gives me
[{1}]
[{3}]
[{1}]
My question is "Why does arr2
not contain the same instance of an Object as arr
?" I believe I understand make
only instantiates a Group
thing which means it includes one Object
in it. But then the for loop shouldn't create a new Object
should it?
Thanks for the help!