Thread about why I said that init() functions in #Golang are evil...

#Golang #Go #Init #Code
1. Init() functions are run before any function in the package, from the first view, it is good to use them as package constructor. In reality, package initializing via init() function will add complexity for your tests, understanding application flows, extendibility of code, etc
2. For example, you decided to init DB connection in the init() function. After this decision, you cannot simply mock DB in your package (or use another init() function in your tests)
3. Let's imagine you have several init() functions in your project. They will be run in "imports order". For example, main.init()->storage.init()->db.init() will be run in order from last to first. What will be if you have ten or more init() functions? I'll call it "init() hell".
4. Moreover, #Golang does not guaranty order of init() functions runs if these functions are located in "tails" of dependence map
5. So, a lot of init() functions make you code untestable and unreadable. With init() functions interface mocking became a rocker science with magic inits, env variables, etc
6. If your library uses init() function for initializing (instead of NewSmth method) - it's really hard to use your library with custom configuration.
7. Cases where init() functions in #Golang are ok:
- single init() function near main() function
- for reading environment variables (but I prefer flags)
8. Cases where init() functions in #Golang are bad:
- create a database connection
- initialization of cache
- initialization network connections
9. Use dependency injection instead (probably without DI container)! Create all required objects with parameters form ENV in your main() function. Push dependencies in the constructor.
10. For example, you have a dependency map main->service->storage->db. Your code int main() function will be like this:

// todo: check errors
db, err := database.NewDB(url, user, password)
store, err := storage.NewStorage(db)
service, err := service.Start(store)
11. So, use init() functions carefully

#Golang #Go

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Taras Sheremeta

Taras Sheremeta Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!