Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to use SQL functions with Go context as parameter?

Tags:

sql

go

I have RESTFUL web service in Golang and I'm using it's SQL driver for database. my question is should I use SQL functions with context as parameter or without it? For instance, there are both QueryContext and Query methods.

Actually I do know what is the context in Go, I just don't know why I should send it to the database methods.

like image 846
taraf Avatar asked Oct 29 '25 10:10

taraf


1 Answers

Context aware methods allow for cancellable query execution.

The way the cancellation is handled is driver specific, but even if the driver is not directly supports taking the context any query that was started with a context should be closed when the context is closed.

This should help with potential connection leaks and query specific timeouts.

like image 103
peterm Avatar answered Oct 31 '25 00:10

peterm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!