This is a creation in Article, where the information may have evolved or changed. # # Goroutine ID does it really exist? Of course there is. There must be some way for the Go runtime to track the Goroutine ID. # # So should I use them? Shouldn't. -reason one: https://groups.google.com/forum/#!topic/golang-nuts/Nt0hVV_nqHE-reason two: https://groups.google.com/forum/#! Topic/golang-nuts/0hgycorhuui-reason three: http://stackoverflow.com/questions/19115273/ looking-for-a-call-or-thread-id-to-use-for-logging## are there any packages that I can use? Existing packages from Go team members are evaluated as "[with this package, will go into hell. ] (HTTPS://GODOC.ORG/GITHUB.COM/DAVECHENEY/JUNK/ID) "There are also packages based on the Goroutine ID to establish goroutine local storage, such as:-[github.com/jtolds/ GLS] (HTTPS://GITHUB.COM/JTOLDS/GLS)-[Github.com/tylerb/gls] (HTTPS://GITHUB.COM/TYLERB/GLS) but all are contrary to the design principles of the Go language. # # The simplest code if read here, you still "stubborn", then the following will show how to get the current Goroutine ID: # # # Go Source of the hacker (hacky) code The following code originates from Brad Fitzpatrick's [' HTTP/2 '] (https:// GITHUB.COM/GOLANG/NET/BLOB/MASTER/HTTP2/GOTRACK.GO) Library. It is integrated into go 1.6 and is only used for debugging rather than conventional development. "' Gopackage mainimport (" bytes "" FMT "" Runtime "" StrConv ") func main () {FMT. Println (Getgid ())}func Getgid () UInt64 {b: = make ([]byte, +) b = b[:runtime. Stack (b, false)] B = byteS.trimprefix (b, []byte ("Goroutine")) B = b[:bytes. Indexbyte (b, ')] n, _: = StrConv. Parseuint (string (b), ten, Max) return n} "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # The ' HTTP/2 ' library uses debug code to track the connection. But only the Goroutine ID is used for debugging. Debug information can be called by the [' Runtime. Stack (buf []byte, all bool) int '] (https://golang.org/pkg/runtime/#Stack) is obtained, which prints the stack information in the buffer in text form. The first line of the stack information is the following text: "Goroutine # # # # # # # # [...]. Here is the real goroutine ID. The remaining code is just some textual manipulation to extract and parse the numbers in the stack information. # # # CGo version corresponding to the legal code C version of the code from [github.com/davecheney/junk/id] (HTTPS://GITHUB.COM/DAVECHENEY/JUNK/TREE/MASTER/ID). The code directly obtains the Goid property of the current Goroutine and returns its value. File ' id.c ' C#include "runtime.h" Int64 Id (void) {return g->goid;} ' File ' id.go ' gopackage idfunc ID () Int64 ' # # # What should I do? Stay away from the Goroutine ID, and forget about their existence. From the perspective of the Go language design, it is dangerous to use them. Because almost all of the purpose of use is to do something related to goroutine-local. This violates the "[Share Memory by communicating] (https://blog.golang.org/share-memory-by-communicating)" Principle of Go language programming.
via:http://blog.sgmansfield.com/2015/12/goroutine-ids/
Author: Scott Mansfield Translator: maleicacid proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
850 reads