The ' Go Tool vet-copylocks ' command Introduction to the Go Language installation package comes with [vet] (https://golang.org/cmd/vet/) command-line tools. The tool can run a set of heuristic algorithms on the program source to discover suspicious program structures, such as code that cannot be executed or to ' FMT '. The wrong call to the Printf ' function (meaning arguments is not aligned with the format parameter): ' Gopackage mainimport "FMT" Func f () {fmt. Printf ("%d\n") return to FMT. Println ("Done")} "" ' > Go Tool vet vet.govet.go:8: Unreachable codevet.go:6: Missing argument for Printf ("%d"): forma T reads Arg 1, with only 0 args ' ', this article specifically describes the tool's copylocks option. Let's see what it can do and how it works in the actual program. Suppose the program synchronizes with a mutex: ' Gopackage mainimport ' sync ' type T struct {lock sync. Mutex}func (t *t) Lock () {T.lock.lock ()}func (T T) Unlock () {t.lock.unlock ()}func main () {t: = T{lock:sync. mutex{}} t.lock () T.unlock () T.lock ()} ' > If the variable v is addressable and the &v method collection contains m, then V.M () is a shorthand for (&v). m (). Think about what the results of the above program might be ... The program enters the deadlock state: ' ' Fatal Error:all Goroutines is asleep-deadlock!goroutine 1 [Semacquire]:sync.runtime_semacquire ( 0X4201162AC)/usr/local/go/src/runtime/sema.go:47 +0x30sync. (*mutex). Lock (0x4201162a8)/usr/local/go/src/sync/mutex.go:85 +0xd0main. (*t). Lock (0X4201162A8) ... ' Running the above program gets a bad result because the root cause is to pass receiver by value to the Unlock method, so ' t.lock.unlock () ' is actually called by the copy of lock. It is easy to overlook this, especially in larger programs. The Go compiler does not detect this, as this may be intentional for the programmer. The vet tool comes up ... ' > Go tool vet Vet.govet.go:13:unlock passes lock by Value:main. The T ' option copylocks (enabled by default) detects whether the type with the Lock method (which actually requires pointer receiver) is passed by value. If this is the case, a warning is issued. The Sync pack has an example of using this mechanism, which has a special type named NoCopy. To avoid a type-by-value copy (actually detected by the Vet tool), you need to add a field (such as Waitgroup) to the struct definition: ' gopackage mainimport ' sync ' type T struct {WG Sync. Waitgroup}func Fun (t) {}func main () {t: = T{sync. waitgroup{}} "Fun (t)}" "> Go tool vet lab.golab.go:9: Fun passes lock by Value:main. T contains sync. Waitgroup contains sync.nocopylab.go:13:function call copies lock Value:main. T contains sync. Waitgroup contains sync.nocopy ' ' in-depth understanding of the mechanism! [Under-the-hood] (https://raw.githubusercontent.com/studygolang/gctt-images/master/Detect-Locks-Passed-by-Value-in-Go/ Under-the-hood.jpeg) The source file for the Vet tool is placed under the '/src/cmd/vet ' path. Each option of vet is registered with the Register function, where two parametersThe number is a mutable parameter (type is the AST node type that the option is interested in) and a callback function. The callback function will be triggered by a specific type of node event. For the Copylocks option, the node that needs to be detected contains a return statement. It eventually goes to Lockpath, which verifies that the passed value belongs to a type (with a Lock method that requires pointer receiver). The Go/ast package is widely used throughout the process. A brief description of the package can be found in the Go source testable example. Click on the "" button below to help others find this article Oh. If you want to get updates about new posts or future work, please follow me here or on Twitter.
Via:https://medium.com/golangspec/detect-locks-passed-by-value-in-go-efb4ac9a3f2b
Author: Michałłowicki Translator: mbyd916 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
197 Reads