-1

So I am using Fiber to build a Rest API and I was wondering on whether or not to run each handler function in a goroutine. For example, say I have the following routes:

router.Get("/get", getMockData)
router.Post("/create", createMockData)
router.Put("/update", updateMockData)
router.Delete("/delete", deleteMockData)

Should I change then to the following:

// I know this isn't syntactically correct but this is just an example. 
router.Get("/get", go getMockData)
router.Post("/create", go createMockData)
router.Put("/update", go updateMockData)
router.Delete("/delete", go deleteMockData)

Should I do this? I have looked at this question and it says no but its targeted towards the net/http package so it doesn't really apply.

Thank you!

darkstar
  • 829
  • 11
  • 23
  • 4
    Each request already runs in its own goroutine, so no. – Burak Serdar Jan 16 '23 at 22:27
  • @BurakSerdar Do you know where exactly thats written in the docs? Or can we assume that because the framework is built on net/http? – darkstar Jan 16 '23 at 23:03
  • Requests are handled in a synchronous function call, ending when that function returns. Think about what would happen if you tried to handle the request in another goroutine. The same answer applies as the linked question. – JimB Jan 16 '23 at 23:41

1 Answers1

1

No. You can't pass a function as a goroutine as this is conceptually wrong. You are passing a reference to a function in router.Get("/get", getMockData) where getMockData is a reference of a function. You are not calling that function as this is not your responsibility. It's the responsibility of Fiber or whatever rest framework you use. When a client hit a API like /get Fiber will call that function and most probably in a separate goroutine for efficiency.

Shahriar Ahmed
  • 502
  • 4
  • 11