see https://go.dev/issue/6703
and https://go.dev/ref/spec#Package_initialization
initialization order is based on lexical references rather than a full
analysis.

- sean

On Sun, Apr 20, 2025, 22:40 Wilgnert <absi...@gmail.com> wrote:

> Hey there! So, I was about to file a GitHub issue, but the prompt
> suggested checking here first to avoid duplicates. I did a quick search for
> "InvalidInitCycle" and "Invalid Init Cycle" in the history and came up
> empty, so hopefully, this is a fresh report.
>
> I'm tinkering with building a little REPL in Go. To handle commands, I set
> up a map where the keys are the command names, and the values are structs
> containing the command's name, a short description, and the actual function
> to execute.
>
> Now, to implement a "help" command, my first instinct was to just iterate
> over this map and print out the name and description of each command.
> However, the Go compiler threw a fit about this.
>
> As a workaround, I came up with a bit of a convoluted solution. I created
> a closure that captures my command map. This outer function then returns
> another function, and *that* inner function finally returns the map.
> Surprisingly, the compiler was perfectly happy with this setup. It compiled
> cleanly, and the "help" command in my REPL works exactly as I intended.
> This closure trick feels a bit dirty, and I'm not entirely sure if I'm
> inadvertently creating some memory leak.
>
> What's really puzzling me is why the compiler would object to the direct
> map iteration in the first place but be perfectly fine with this nested
> function approach. Could someone shed some light on the underlying logic
> here?
>
> For reference, I'm using go version go1.24.2 linux/amd64.
>
> Thanks in advance for any insights!
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/96783b30-aa66-4db4-895a-d16ce3ff4807n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/96783b30-aa66-4db4-895a-d16ce3ff4807n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPpZNy3%3DwW11rpj%2BTbKiXjOa6PwF_tMhAbDP_EbRjKzaYg%40mail.gmail.com.

Reply via email to