Answer the question
In order to leave comments, you need to log in
Why doesn't the closure work?
There is this test:
func fn(got []string) func(string) {
return func(input string) {
got = append(got, input)
}
}
func TestWalk(t *testing.T) {
cases := getCases()
for _, test := range cases {
t.Run(test.Name, func(t *testing.T) {
var got []string
walk(test.Input, fn(got))
if !reflect.DeepEqual(got, test.ExpectedCalls) {
t.Errorf("got %v, want %v", got, test.ExpectedCalls)
}
})
}
}
walk(test.Input, func(input string) {
got = append(got, input)
})
Answer the question
In order to leave comments, you need to log in
Because not by reference, but by value . Those. the length, capacity, and pointer to slice data are copied, but the data itself is not.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question