使用Java的时候很容易得到线程的名字， 比如"Thread.currentThread().getName"，这样就可以进行一些监控操作或者设置线程相关的一些数据。当转向Golang开发的时候，却发现Go语言并没有提供获取当前goroutine id的操作。这是Golang的开发者故意为之，避免开发者滥用goroutine id实现goroutine local storage (类似java的"thread-local" storage)， 因为goroutine local storage很难进行垃圾回收。因此尽管以前暴露出了相应的方法，现在已经把它隐藏了。
Please don't use goroutine local storage. It's highly discouraged. In fact, IIRC, we used to expose Goid, but it is hidden since we don't want people to do this.
Potential problems include:
- when goroutine goes away, its goroutine local storage won't be GCed. (you can get goid for the current goroutine, but you can't get a list of all running goroutines)
- what if handler spawns goroutine itself? the new goroutine suddenly loses access to your goroutine local storage. You can guarantee that your own code won't spawn other goroutines,
but in general you can't make sure the standard library or any 3rd party code won't do that.
thread local storage is invented to help reuse bad/legacy code that assumes global state, Go doesn't have legacy code like that, and you really should design your code so that state is passed explicitly and not as global (e.g. resort to goroutine local storage)
当然Go的这种隐藏的做法还是有争议的，有点因噎废食。在debug log的时候goroutine id是很好的一个监控信息。本文介绍了两种获取goroutine id的方法。