-2

I am new to Go and used to be a javaer. Recently I came along such a problem,like the code below, the first snippet will cause assignment problem while the second will not.

But I don't know why.

desiredAcls := make([]*kafkav1alpha2.Acl, 0)
for _, acl := range instance.Spec.Authorization.Acls {
    desiredAcls = append(desiredAcls, &acl)
}

desiredAcls := make([]*kafkav1alpha2.Acl, 0)
for _, acl := range instance.Spec.Authorization.Acls {
    cpyAcl := acl
    desiredAcls = append(desiredAcls, &cpyAcl)
}

In constrast to Java,there will be no problem and I am very free to write code cause the life cycle of loop body variables limits to a single loop.

List<Object> acls = new ArrayList<>();
List<Object> desiredAcls = new ArrayList<>();
for (Object acl : acls) {
   desiredAcls.add(acl);
}

So why doesn't golang limit the life cycle of loop body variables to a single loop? this kind of optimization can reduce a lot of coding burden for engineers.

Leslie__Joe
  • 53
  • 1
  • 7
  • why do you say `So Why didn't Go abolish Pointers?` ? –  Aug 10 '21 at 15:55
  • Does this answer your question? [Go loop pointer changes](https://stackoverflow.com/questions/32401933/go-loop-pointer-changes) –  Aug 10 '21 at 15:57
  • The problem is with how for-loop works, not pointers. For-loop overwrites the range variables. Java has pointers as well, but not objects by value. – Burak Serdar Aug 10 '21 at 16:00
  • @BurakSerdar Java does have Pointers but they're transparent to me. And Java doesn't have a problem with the same code for range variables. – Leslie__Joe Aug 10 '21 at 16:18
  • @BurakSerdar Because I don’t know when to use Pointers and when to use Values, I always use Pointers like Java when coding.I don't know if it's okay. – Leslie__Joe Aug 10 '21 at 16:24
  • @mh-cbon Thanks for your quick reply. I already know what the problem is. At first I thought it was the pointer that caused the value of the for-loop variable to not be released.But isn’t the life cycle of loop variables limited to one loop? – Leslie__Joe Aug 10 '21 at 16:33
  • you mean the scope of the body loop ? Your variable escape to the heap, the compiler figures that out. At the risk of doing it wrong, compiler optimize a lot, if you run escape analysis upon https://play.golang.org/p/r7PsE7bfujY you will see thee `./main.go:11:9: moved to heap: v` my go version is 1.15.XX t might be different on using another version (i disabled inlining with `go build -gcflags="-m -l"`) –  Aug 10 '21 at 16:49
  • @Leslie__Joe if you always use pointers, you're forcing a lot of inefficiencies. Use pointers only when you need pointer semantics. Go is very, very different from Java, and it will hinder your Go work to think of it in relation to Java concepts and practices. – Adrian Aug 10 '21 at 16:56

1 Answers1

3

But I don't know why.

Because in the first example, you're taking a pointer to the loop variable. Each iteration doesn't create a new variable, it overwrites the value of the same loop variable, so every iteration you're taking a pointer to the same memory.

So Why didn't Go abolish Pointers?

Pointers are useful. But to really answer this question, you'd have to ask the Go language designers.

Adrian
  • 42,911
  • 6
  • 107
  • 99