GoScope (goscope
) and rgs are two command-line tools designed to help Go developers quickly locate and navigate to functions, variables, constants, and types in Go projects. GoScope analyzes all .go
files starting from the current directory (recursively), while rgs provides an interactive interface (via fzf) to jump straight into your editor at the relevant line.
- Automatic Scanning: GoScope scans the current directory and all subdirectories for
.go
files. - Comprehensive Listing: It outputs:
- Declared functions (with file and line number).
- Declared variables and constants (with file and line number).
- Declared types (with file and line number).
- Function call occurrences in the format
Caller.Callee file:line
.
- Interactive Search: The
rgs
script leveragesfzf
to allow fuzzy-searching any function, variable, constant, or type, and then opens the file at the exact line in your preferred text editor.
- GoScope
- Go 1.23 or later (to compile the tool).
- A Unix-like shell environment (macOS, Linux, etc.).
- rgs
- Clone or Download this repository.
- Build GoScope:
This produces an executable named
cd path/to/this/repo go build -o goscope
goscope
. - Install GoScope (optional but recommended):
Make sure
mv goscope /usr/local/bin/
/usr/local/bin
is in your$PATH
. - Install rgs:
Again, confirm
chmod +x rgs cp rgs /usr/local/bin/
/usr/local/bin
is in your$PATH
.
- Simply run
goscope
in the terminal. - It scans the current directory recursively and prints all findings:
- Functions, variables, constants, and types in the format:
FunctionName file.go:line VariableName file.go:line ConstantName file.go:line TypeName file.go:line
- Followed by function call mappings in the format:
CallerName.CalleeName file.go:line
- Functions, variables, constants, and types in the format:
- There are no command-line parameters for GoScope: it always starts scanning from the current directory.
- In the terminal, run:
rgs
- This calls
goscope
, then pipes its output intofzf
. - You can type partial names to filter results.
- Use the arrow keys or your usual
fzf
navigation to select an item and press Enter. rgs
then opens the corresponding file at the line where the item is declared (or called), using your editor set by$EDITOR
.
Below is a simple Go code snippet to show how GoScope output might appear:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
PrintNumber(42)
}
func PrintNumber(num int) {
fmt.Printf("Number: %d\n", num)
}
Sample GoScope output:
main main.go:5
PrintNumber main.go:10
main.Println main.go:6
main.PrintNumber main.go:7
PrintNumber.Printf main.go:11
Using rgs:
- In your project directory, type:
rgs
- Type
PrintNum
in the search prompt, and select the result. - Press Enter to open the file at the corresponding line.
- Ensure
goscope
is in your$PATH
so thatrgs
can invoke it properly. - If
bat
is not installed, you may remove or modify the preview command inrgs
. - If
$EDITOR
is not set, you can either set it before runningrgs
, or update the script to call your favorite editor directly.