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 golang-nuts+unsubscr...@googlegroups.com <mailto: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/2915b008-4145-4633-a0d5-124cbe50e546%40gmail.com.

Reply via email to