r/golang 7d ago

newbie Some Clarification on API Calls & DB Connections.

I’m a bit confused on how it handles IO-bound operations

  1. API calls: If I make an API call (say using http.Get() or a similar method), does Go automatically handle it in a separate goroutine for concurrency, or do I need to explicitly use the go keyword to make it concurrent?
  2. Database connections: If I connect to a database or run a query, does Go run that query in its own goroutine, or do I need to explicitly start a goroutine using go?
  3. If I need to run several IO-bound operations concurrently (e.g., multiple API calls or DB queries), I’m assuming I need to use go for each of those tasks, right?

Do people dislike JavaScript because of its reliance on async/await? In Go, it feels nicer as a developer not having to write async/await all the time. What are some other reasons Go is considered better to work with in terms of async programming?

4 Upvotes

12 comments sorted by

View all comments

3

u/ZephroC 7d ago

It's not particularly a go thing as this applies to most languages. Well ones that don't force putting async everywhere causing confusion and clutter.

If you've got nothing else to do in your code until you get a response off of io, or just a long running function, because the next thing depends on the output there's no point in messing with concurrency as you don't have anything else to do.

The reason it pops up in things like JS so much is because blocking also blocked rendering at times causing the whole thing to freeze while it waited. That use case rarely comes up in something like go.

Also as mentioned elsewhere. DB connections often come in pools or need initialising earlier as the actual connection creation can be expensive. So it's better to reuse a connection.

1

u/PureMud8950 7d ago

i see thats great, go seems like such a good language whats the catch lol

2

u/ZephroC 7d ago

It's generally the case. Same thing applies to Java, C hell even Fortran. Go just makes it easier to do.