Compiler Hiccup with Map Iteration

82 views
Skip to first unread message

Wilgnert

unread,
Apr 20, 2025, 5:41:33 PMApr 20
to golang-nuts

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!

compiles.go
does_not_compile.go

Mikk Margus

unread,
Apr 20, 2025, 5:48:13 PMApr 20
What is the actual error the compiler is reporting? Are you perhaps
trying to iterate over a pointer to a map or something like that?

On 4/20/25 23:55, Wilgnert 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 [email protected] <mailto:golang-
> [email protected]>.
> 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>.

Mikk Margus

unread,
Apr 20, 2025, 6:09:43 PMApr 20
Oh, whoops, I did not realize the attachments contained examples...
I suppose you could delay the commandRegistry assignment to be within
`func init()`
Attached is an example of this.


On 4/20/25 23:55, Wilgnert 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!
>
main.go

Sean Liao

unread,
Apr 20, 2025, 7:44:21 PMApr 20
to golang-nuts
initialization order is based on lexical references rather than a full analysis.

- sean

--
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/96783b30-aa66-4db4-895a-d16ce3ff4807n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages