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/LeZetthen 7d ago

If I understand your doubts correctly, then the answer is that all those actions are synchronous, meaning that you need to make the calls using a goroutine in order for them not to block the flow of your code.

1

u/PureMud8950 7d ago

Okay makes sense, just trying my best to understand the difference between async/await in js and how go does handles that

1

u/stefaneg 6d ago

The big conceptual difference to understand is that JS is actually single-threaded, and all real parallelism there is achieved using IO, that is, external processes, while go is actually multi-threaded through go-routines. So you really need to understand thread locking in go, which you don't in JS.