This is a creation in Article, where the information may have evolved or changed.
Translation Source: Http://golang.org/s/using-guru
Guru is an editor integration tool that navigates the go code
This article will show how we use the Guru. During the development of the Golang project, we wanted our editor to answer all our questions. For example:
Where is the identifier (name, for variables, constants, functions, statement blocks) declared?
Where are all the references to this statement?
What is the method variable for this expression?
What is the API for this package?
What specific types implement this interface?
What are the possible callers for a dynamic call?
Method possible call what is this?
Where may the value sent on this channel be received?
In addition to the advanced static analysis features that are not found in any IDE, it also brings a lot of heavy-weight Ides to bring you exciting results.
Gophercon talk on the Go Guru (Https://www.youtube.com/watch?v=ak97oH0D6fI).
Domestic video address (http://www.tudou.com/programs/view/VQgpql8a8fk/)
Guru supports more than 10 types of queries. Place your cursor (or text selection) on the variable name or the expression you are interested in, and then execute the Guru command query through your editor.
Depending on your editor, choose whether you want to use a menu or a command line. The results of queries against most query directives are cached in the editor. Take a look at the video below.
Setting up
Queries
Name Queries
What
Definition
Referres
Freevars
Type Queries
Describe
Implements
Call Graph queries
Callees
Callers
CallStack
Alias queries
Pointsto
Whicherrs
Peers
Setting up
Download and compile the Guru tool and then insert it into your. Under Path paths
$ go get Golang.org/x/tools/cmd/guru
$ go build Golang.org/x/tools/cmd/guru
$ mv Guru $ (Go env GORO OT)/bin (for example)
$ guru-help
Go source code guru. Usage:guru [flags] <mode> <position>
For many guru queries, the workspace that defines the package must be configured with $gopath environment variables. Although Guru is a command-line tool, it needs to be done with your editor. The output of most guru queries is a diagnostic list, with each prefix having the source file name, line number, and column number. Similar to the diagnostic output of the general editor. Most editors can display the edit output in a buffer so that click Diagnostics can open the relevant source file in the correct location. Please refer to the corresponding editor documentation to complete the installation.
editor |
link |
Sublime text |
Https://alvarolm.github.io/GoGuru Installation Documentation: HTTP://WWW.JIANSHU.COM/P/7785511097EF |
Acme |
H ttps://github.com/davidrjenni/a Https://github.com/mjibson/aw |
atom.io |
HTTPS ://atom.io/packages/go-oracle (not available now) |
Eclipse |
https://github.com/GoClipse/goclipse |
Emacs |
https://github.com/dominikh/go-mode.el HTTPS://GITHUB.COM/GOLANG/TOOLS/BL Ob/master/cmd/guru/go-guru.el |
Vim |
https://github.com/fatih/vim-go |
Vscode |
https://github.com/fatih/vim-go |
If your editor does not appear in the list above, try to help support it with your ideas. Most queries can be supported by a small amount of existing mechanisms that reuse the editor to run the compiler's logic. This logic must construct a command that specifies the desired type of query and the current cursor position, and directs the output of the command to the editor buffer.
Some features require additional editor-specific logic. Guru can parse the modified but unsaved editor buffers, but this requires the editor to provide the modified buffer's contents to Guru's standard input. The editor can automatically highlight the same identifier as the current selection identifier each time the cursor moves. Or it can jump directly to the variable declaration after the definition query. When the-json flag is provided, the Guru emits output in JSON format; This makes it easier for editors to parse the output of the tool.
pointer Analysis Scope : Some queries involving pointer analysis, one for answering "what might the pointer point to?" "The problem of technology. Running pointer analysis on all packages in Workspace is often too resource-intensive, so these queries require additional configuration parameters for a qualifying zone to parse the package set for the range specified by the parameter. Set the scope to the application (or the application set, possibly the client and server) on which you are currently working. Pointer analysis is an overall program analysis, so the only package within the scope is main and the test package. A range is typically specified as a comma-delimited set of packages, or as Github.com/my/dir/... Such a pass-gamete tree; Consult the editor's specific documentation to learn how to set and change the scope.
Queries
Guru support for more than more than 10 queries, listed below for help information
callees |
displays the possible targets for the selected function call |
Callers |
Displays the possible callers for the selected feature |
CallStack |
Displays the path from the root directory to the selected function from the call graph |
Definition |
Displays the declaration of the selected identifier |
Describe |
Describe the selected syntax: Definitions, methods, etc. |
Freevars |
Show free variables for selection |
Implements |
Displays the implementation relationship for the selected type or method |
Peers |
Displays the Send/receive corresponding to the selected channel operation |
Pointsto |
Displays the variables that the selected pointer might point to |
Referrers |
Displays all references represented by the selected identifier |
What |
Displays basic information about the selected syntax node |
Whicherrs |
Displays the possible values for the selected error variable |
We will divide these queries into four groups of related attributes---name, type, call graph and alias---Use this program as a running example:
11111.png
Name queries
A Names (identifier)-related claim reference. The first set of Guru queries helps you navigate from the reference to the Declaration and back again.
What
What query locates the current source location as quickly as possible. As long as the cursor position changes. The user does not need to do anything, as long as the mouse position changes the editor will make corresponding feedback. It can be used to highlight all identifiers that are identical to the current screen, as shown in this example screen:
Definition
Defines the declaration of the query to find the selected identifier. In some editors, it can jump the cursor directly to that position.
Example: In this vedio, we use a contiguous definition query to find HTTP. The URL field of the request, the type URL. URL and type Url.userinfo.
Referrers
The referrers query finds a reference to the selected identifier and scans all related packages in the workspace.
Example: In this vedio, we use referrers to find all references to the handler type, and to HTTP in the entire workspace. All references to the request.
Freevars
Freevars queries use enumerations to select free variables. A "free variable" is a technical term that refers to a set of variables that are referenced in the selection but not defined, or simply, its input. This information is useful if you are considering whether to refactor the selected statement into your own function, because the free variable is the necessary parameter for the function. It is also useful when you want to understand the input of a complex code block, even if you do not intend to change it.
To make the results more useful, the output of the query is slightly different from the free variables defined by the textbook:
- The output does not report any names defined at the package level, because they do not need to be passed as parameters to the function;
- For each free structure variable,
- The output also reports a reference to free constants and types, that is, it reports all free names, not just variables
Example: In this video, a freevars query about more than 10 rows of the FMT package shows that the selected statement block depends on several parts of Typestring,v and P. If we extract it as a separate function, we may make it a * pp method with the following signature:
> func (p *pp) fmtBytesSharpV(typeString string, v []byte)
Type queries
This group of queries answers questions about the type of expression.
Describe
The describe query shows the various properties of the selected syntax: its syntax, type of expression, value of constant expression, size, alignment, method set and type interface, declaration of identifiers, etc. you can describe almost any syntax, Guru will print all the useful information. Description of the package (for example, FMT. The FMT in println includes all of its exported members, their types, methods, and values (for constants). If the current package is described (by selecting Package-declaration package p), the description also includes non-exportable.
Example: In this video, we describe the handler type, Expression W. Header (), HTTP. The URL field of the request, and finally the log package.
Implements
The implements query displays the interface implemented by the selected type, and if the selected type is itself an interface, displays a collection of specific types that implement it. The implementation of a value query reports the same information about an expression type. The implements query for a method displays a collection of abstract or concrete methods associated with it.
Example: In this video, we list the interfaces implemented by the type handler, and the type is HTTP. Responsewriter and Value Req.url.
Call Graph queries
This query answers the question about the call graph, which is the relationship between the function call point and the called function. For a static function or method call, the relationship is obvious, but for dynamic invocation, the master must use pointer parsing. Therefore, you must configure the scope before you use these queries.
Callees
The callee query displays the possible call targets for the selected feature call site. The cursor or selection must be in the function call expression; The selection does not have to be precise. This query uses pointer parsing to model the impact of dynamic function calls, so it requires a scope.
Example: Calling the caller of an interface method query W. The Header () tells us that in this application, the call may dispatch two possible concrete methods, one for the HTTP V1 and the other for the HTTP v2.
Callers
The caller's query displays the possible callers that contain the selected function. This query uses pointer parsing to model the impact of dynamic function calls, so it requires a scope.
Example: Although there is no pair of handler. The direct invocation of the Servehttp method calls the query to show that it is dynamically called by the HTTP Server package (because it is a registered handler).
CallStack
The callstack query displays from the root of the call graph to any path that contains the selected function. This may help to understand how the function is reached in a given program. This query uses pointer parsing to model the impact of dynamic function calls, so it requires a scope. The accuracy and usefulness of the call stack information varies greatly, especially if the calling path contains a greater degree of dynamic invocation. The selected call stack may not be feasible, that is, it will never occur during any actual execution.
Example: This video is displayed to handler. The callstack query for the Servehttp function. It steps up the frame of the generated call stack through several functions in the HTTP server, and then arrives at the Listenandserve call in the keynote.
Alias queries
An alias query answers questions about pointers (and other reference types, such as slices, maps, functions, and channels) and what they point to. Like the call graph query, an alias query uses pointer parsing, so the scope must be configured before the scope of the call.
Pointsto
The Pointsto query displays a collection of possible objects that the pointer may point to. It also applies to other reference types such as slices, functions, maps, and channels. This query uses pointer parsing, so you must specify a range.
Example: In this video, the Pointsto query tells us that HTTP is in this program. Responsewriter W may contain two possible dynamic types: http.http2responsewriter and http.response. In addition, each pointer points to a variable created by a composite literal expression ("Complit"). Click to bring us to the location where you created the Http.response variable.
Whicherrs
The Whicherrs query reports a set of possible constants, global variables, and specific types that may occur in a value of type error. This information can be useful when handling errors to ensure that all important situations are handled.
Example: for HTTP. The Whicherrs query that listenandserve the result of the call shows that the possible error is einval (programmer error), System call error, or network error.
Peers
The peer query displays a possible send/Receive collection on the channel operand of the selected send or receive operation; Select must be <-token. This query uses pointer parsing, so you must specify a range.
Example: A peer query on a send statement in a counter function reports that the channel is assigned in the main processor and is in the processor. The Servehttp method is received.