Had the following doubt while thinking of adding a init() func to main.go (to define scalar types available globally):
Not sure if we already do this kind of thing, if we do, please point me to it.
- Suppose we want to declare global vars (similar to postingDir, uidDir declared in main.go) but we also want to do error handling with them.
- We don’t want to declare them in main.go (don’t want to clutter it), but initialize them before it’s execution starts.
- We want to define those variables in the package where their parent type/struct is defined.
What would be the best way to do this?
- Maintain a init() method in main.go
- Declare required global vars in the intended package
- Define those vars in a func inside that file
- Handle errors
- Call this function from init() inside main.go
Would love to hear everyone’s thoughts on this.