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

That's idiomatic go. See https://go.dev/blog/generate and '//go:generate stringer -type=Pill'

See also '//go:embed', which also turns a variable into a magic type via an ad-hoc comment-only DSL.

In go, it is idiomatic to say "macros slow down compilation and often require a second pass, go compilation is fast, macros are bad", and then also to have an extra "make sure go generate is up to date" CI step which parses your go codebase 10 more times, forks dozens of processes, and isn't type-safe since of course it's not it's literally a comment you can typo "//gog:enerate" and no one will notice.




Worth noting that most go codebases don't contain magic comments like this.

I've certainly never used them and agree they are horrible in every way.


I frequently tell people that Go is my least favorite language for reasons like this and the response is often some form of the no true Scotsman fallacy.




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

Search: