Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I hate that forgetting to initialize all fields of a struct does not lead to an error, but will just fill in "zero-values" for those fields. This has bitten me many times, and zero-values are almost never the behavior I want.

If empty values are biting you, you need constructors.

There's definitely multiple ways to go about struct initialization, and Go had firmly chosen one approach. Best practice is to make the zero value of a field "useful", and to write a constructor function if you need it populated with any sort of conditional values.



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: