Does Go have something like ThreadLocal from Java?

不想你离开。 提交于 2019-12-09 14:53:54

问题


I use Go and Gin to setup my website and want to know the database access time. I use goroutine so if don't use something like thread-local, I must change almost every function to do it. Does Go have a good way to do it?


回答1:


The Go runtime and standard libraries do not provide goroutine local storage or a goroutine identifier that can be used to implement goroutine local storage.

The third party gls package implements goroutine local storage in an interesting way. Some find this package horrifying and others think it's clever.

The Go team recommends passing context explicitly as function argument instead of using goroutine local storage. See the context blog post and package documentation for more information.



来源:https://stackoverflow.com/questions/31932945/does-go-have-something-like-threadlocal-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!