在Go中是否有静态方法来获取当前请求/上下文?

I want my logging methods to output some sort of request ID. To do that I need to pass a request object (or some derivative) to the logging functions.

This is all fine when logging from the request handlers, but is problematic when used from within library-style methods, as I don't wish those to be aware of any ongoing http-requests.
Furthermore, those same library methods might be used in the future by console applications, and than the request-id would be replaced by some sort of worker-thread-id.

Using a context solves the problem, but this means I will have to add a context parameter to all my methods, which is somewhat annoying.

So, basically, what I need is some sort of static storage that is passed between method and goroutine calls.

I'm not sure there's anything like that in Go, so maybe my approach is totally off-base, in that case, I would be happy to hear what is a better approach to solve the above problem.

Thanks,

Try another logging library. For instance, in log15 (https://github.com/inconshreveable/log15) a Logger as an embedded key/value context.

logger := log.New("request_id", "8379870928")

You can pass the logger object to anyone that needs to log. Later on:

logger.Warn("blabalbla")

... will embed the request_id that you have put.