Go是否具有Java的ThreadLocal之类的东西? [英] Does Go have something like ThreadLocal from Java?

查看:126
本文介绍了Go是否具有Java的ThreadLocal之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Go和Gin来设置我的网站,并想知道数据库访问时间.我使用goroutine,因此,如果不使用本地线程之类的东西,则必须更改几乎所有函数才能使用. Go可以做到吗?

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?

推荐答案

Go运行时库和标准库不提供goroutine本地存储或可用于实现goroutine本地存储的goroutine标识符.

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.

第三方 gls 包以一种有趣的方式实现了goroutine本地存储.有些人觉得这个软件包令人恐惧,而另一些人则认为它很聪明.

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

Go小组建议显式传递上下文作为函数参数,而不是使用goroutine本地存储.请参见上下文博客文章包装文档以获取更多信息.

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.

这篇关于Go是否具有Java的ThreadLocal之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆