If this is your first time using the Go Doctor, see the Go Doctor repository on GitHub for basic instructions.
You will probably want to run the Go Doctor from Vim, since it allows you to rename variables, extract functions, and perform other refactorings with just a few keystrokes. Detailed information about the Vim plug-in is available in the Vim Plug-in Reference. If you are inclined to use the command-line godoctor tool directly, the godoctor man page contains several examples illustrating typical invocations.
Later parts of this user manual contain detailed information on each of the refactorings supported by the Go Doctor.
The latest documentation for the Go Doctor (including this user manual, the godoctor man page, and the Vim Plug-in Reference) is hosted via GitHub Pages. If you want to install a local copy of the documentation, see below.
If you do not want to use the online documentation, you can install the Go Doctor documentation locally. The godoctor tool generates its own documentation. It can generate a local copy of the Installation Guide and User Guide on the Go Doctor Web site; it can generate a Unix man page for the godoctor tool; it can also generate more complete documentation for the Vim plug-in (by default, the Vim plug-in includes stub documentation pointing to the Go Doctor Web site).
sudo mkdir -p /usr/local/share/man/man1
sudo sh -c 'godoctor -doc man >/usr/local/share/man/man1/godoctor.1'
godoctor -doc vim >~/.vim/godoctor.vim/doc/godoctor-vim.txt
:helptags ~/.vim/godoctor.vim/doc
The Rename refactoring is used to change the names of variables, functions, methods, and types. Package renaming is not currently supported.
An error or warning will be reported if:
The example below demonstrates the effect of renaming the highlighted occurrence of hello to goodnight. Note that there are two different variables named hello; since the local identifier was selected, only references to that variable are renamed, as shown.
| Before | After | |
|---|---|---|
package main
import "fmt"
var hello = ":-("
func main() {
hello = ":-)"
var hello string = "hello"
var world string = "world"
hello = hello + ", " + world
hello += "!"
fmt.Println(hello)
}
|
⇒  |
package main
import "fmt"
var hello = ":-("
func main() {
hello = ":-)"
var goodnight string = "hello"
var world string = "world"
goodnight = goodnight + ", " + world
goodnight += "!"
fmt.Println(goodnight)
}
|
The Extract Function refactoring creates a new function (or method) from a sequence of statements, then replaces the original statements with a call to that function.
The refactoring will automatically determine what local variables need to be passed to the extracted function and returned as results.
An error or warning will be reported if the selected statements cannot be extracted into a new function. Usually, this occurs because they contain a statement like return which will have a different meaning in the extracted function.
The Extract Local Variable takes an expression, assigns it to a new local variable, then replaces the original expression with a use of that variable.
An error or warning will be reported if the selected expression cannot be extracted into a variable assignment. For example, this could occur if the extracted expression is in a loop condition but its value may change on each iteration of the loop, or if the extracted variable's name is the same as the name of an existing variable.
The example below demonstrates the effect of extracting the highlighted expression into a new local variable sum.
| Before | After | |
|---|---|---|
package main
import "fmt"
func main() {
y := 2
z := 3
if x := 5; x == y + z {
fmt.Println(x)
}
}
|
⇒  |
package main
import "fmt"
func main() {
y := 2
z := 3
sum := y + z
if x := 5; x == sum {
fmt.Println(x)
}
}
|
The Toggle var ⇔ := refactoring converts a var declaration to a short assignment statement (using :=), or vice versa.
If a var declaration is selected, it will be converted to a short assignment statement (:=). If a short assignment statement is selected, it will be converted to a var declaration (with an explicit type declaration).
An error or warning will be reported if the selected statements cannot be converted. For example, declarations at the file scope must be declared using var; they cannot be converted to short assignment statements.
The example below demonstrates the effect of toggling the highlighted declaration of msg between a short assignment statement and a var declaration.
| Before | After | |
|---|---|---|
package main
import "fmt"
func main() {
msg := hello
fmt.Println(msg)
}
|
⇔  |
package main
import "fmt"
func main() {
var msg string = hello
fmt.Println(msg)
}
|
This refactoring searches a file for exported declarations that do not have GoDoc comments and adds TODO comment stubs to those declarations.
The refactored source code is formatted (similarly to gofmt).
This refactoring is applied to an entire file. It does not require any particular text to be selected, and it does not prompt for any additional user input.
In the following example, Exported, Shaper, and Rectangle are all exported but lack doc comments. This refactoring adds a TODO comment for each.
| Before | After | |
|---|---|---|
package main
import "fmt"
func main() {
Exported()
}
func Exported() {
fmt.Println("Hello, Go")
}
type Shaper interface {
}
type Rectangle struct {
}
|
⇒ |
package main
import "fmt"
func main() {
Exported()
}
// Exported TODO: NEEDS COMMENT INFO
func Exported() {
fmt.Println("Hello, Go")
}
// Exported TODO: NEEDS COMMENT INFO
type Shaper interface {
}
// Exported TODO: NEEDS COMMENT INFO
type Rectangle struct {
}
|
.\" Save this as godoctor.1 and process using
.\" groff -man -Tascii godoctor.1
.\" or for PostScript output:
.\" groff -t -mandoc -Tps godoctor.1 > godoctor.ps
.\" or for HTML output:
.\" groff -t -mandoc -Thtml godoctor.1 > godoctor.1.html
.TH godoctor 1 "" "Go Doctor 0.8 (Beta)" ""
.SH NAME
godoctor \- refactor Go source code
.SH SYNOPSIS
.B godoctor
[
.I flag
.I ...
.B ]
.I refactoring
[
.I args
.I ...
.B ]
.SH DESCRIPTION
godoctor refactors Go Source code, outputting a patch file with the changes (unless the -w or -complete flag is specified).
.PP
The Go Doctor can be run from the command line, but it is more easily used from an editor like Vim.
.PP
The Go Doctor is no longer actively maintained; gopls (https://github.com/golang/tools/tree/master/gopls) provides equivalent functionality. The Go Doctor repository remains available at https://github.com/godoctor/godoctor
.SH OPTIONS
The following
.I flags
control the behavior of the godoctor:
.PP
The
.I refactoring
determines the refactoring to perform:
.TP
.B rename
Changes the name of an identifier
.TP
.B extract
Extracts statements to a new function/method
.TP
.B var
Extracts an expression, assigning it to a variable
.TP
.B toggle
Toggles between a var declaration and := statement
.TP
.B godoc
Adds stub GoDoc comments where they are missing
.PP
The
.I args
are specific to each refactoring. For a list of the arguments a particular refactoring expects, run that refactoring without any arguments. For example:
.B godoctor
rename
.SH EXAMPLES
.TP
Display a list of available refactorings:
.B godoctor
-list
.PP
.TP
Display usage information for the Rename refactoring:
.B godoctor
rename
.PP
.TP
Rename the identifier in main.go at line 5, column 6 to bar, outputting a patch file:
.B godoctor
-pos 5,6:5,6
-file main.go
rename
bar
.PP
.TP
Toy example: Pipe a file to the godoctor and rename n to foo, displaying the result:
echo 'package main; import "fmt"; func main() { n := 1; fmt.Println(n) }' | godoctor -pos 1,43:1,43 -w rename foo
.PP
.SH EXIT STATUS
.TP
0
Success
.TP
1
One or more command line arguments were invalid
.TP
2
Help/usage information was displayed; no commands were executed
.TP
3
The refactoring could not be completed; output contains a detailed error log
.SH AUTHOR
See https://github.com/godoctor/godoctor
*godoctor-vim.txt*
*godoctor*
- = = = - ~
T H E G O D O C T O R ~
a golang refactoring tool ~
- = = = - ~
https://github.com/godoctor/godoctor
Vim Plugin Reference Manual ~
==============================================================================
CONTENTS *godoctor-contents*
1.Intro.........................................|godoctor-intro|
2.Commands......................................|godoctor-commands|
3.Global Options................................|godoctor-global-options|
4.License.......................................|godoctor-license|
==============================================================================
1. Intro *godoctor-intro*
The Go Doctor provides source code refactoring for golang programs.
The Go Doctor is no longer actively maintained. gopls
(https://github.com/golang/tools/tree/master/gopls) provides equivalent
functionality and is actively developed. The Go Doctor source and
documentation remain available at:
https://github.com/godoctor/godoctor
This documentation provides a reference for the commands and options unique to
the Go Doctor Vim plugin.
This documentation was generated for Go Doctor 0.8 (Beta).
==============================================================================
2. Commands *godoctor-commands*
:Refactor *:Refactor*
:GoRefactor *:GoRefactor*
The Refactor and GoRefactor commands are synonymous; either can be used to
initiate a Go refactoring. These commands are available only when a Go source
file is being edited.
The command name is followed by the refactoring name and arguments (if any).
For example, to rename an identifier in a Go program to "foo", move the cursor
over the identifier (or select it), and issue the command:
Example: >
:Refactor rename foo
<
Vim will autocomplete the refactoring name ("rename" in the example above).
A list of files modified and errors that occurred (if any) are displayed in the
|location-list|.
:Rename *:Rename*
The Rename command is a shortcut for ":Refactor rename" and is otherwise used
similarly.
==============================================================================
3. Global Options *godoctor-global-options*
*'doctor_scope'*
Default: ""
If this is set, its value is passed to the godoctor via the "-scope" flag.
For example: >
let g:doctor_scope='/path/to/main.go'
<
Usually, the scope will point to the program entrypoint (main package) for the
program you want to refactor. If an explicit scope is not set, the Go Doctor
assumes that the current directory contains a Go package, and it uses that
package as the scope.
==============================================================================
4. License *godoctor-license*
Copyright Auburn University and others. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
vim:textwidth=78:shiftwidth=4:filetype=help:norl:
Copyright © Auburn University and others. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.