Hacker News new | past | comments | ask | show | jobs | submit login

I've recently been bit by global state that used function initializers (func init() isn't the only way to execute logic when a package is imported!). The package used very reasonable defaults, but panic()'d when things didn't look right. So, forking, maintaining patches until upstream changes the behavior, painful.



> func init() isn't the only way to execute logic when a package is imported!

What are other ways to execute logic when a package is imported? init() is the only one I'm aware of.


package globals

var ( X = blah() )

func blah() int { return 123 }


Those are all appended into the Package.init function in the compiled .a, so they are part of func init() not just in behavior, but in implementation as well.


Ha, you're totally right. I'm silly. Thanks!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: