This should be possible, because the slice simply contains the address of the function. Why the compiler cares what the function will be doing when called ? Equivalent code will work in several other languages.
On Friday, October 22, 2021 at 2:59:36 PM UTC-3 Jan Mercl wrote: > testSlice mentions printLen, printLen mentions testSlice. The > specification explicitly forbids such init cycles. > > On Fri, 22 Oct 2021, 19:51 dana...@gmail.com, <dana...@gmail.com> wrote: > >> Why do I get an initialization loop in the following program? >> >> ```go >> package main >> import "fmt" >> var testSlice = []func(){printLen} >> func printLen() { >> fmt.Print(len(testSlice)) >> } >> func main() { >> printLen() >> } >> ``` >> Playground Link: https://play.golang.org/p/tJenMMaWex6 >> Using go1.17.2 >> >> -- >> 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...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/505a239c-441f-41f5-bcc3-93b2fa8f2561n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/505a239c-441f-41f5-bcc3-93b2fa8f2561n%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 on the web visit https://groups.google.com/d/msgid/golang-nuts/7ce8fe44-6a46-4427-8e32-d97b15e4eeebn%40googlegroups.com.