knife is a CLI tool for inspecting Go packages, focusing on listing type and object information.
It provides a -f option similar to go list, allowing you to customize output via Go templates.
Additionally, this repository offers several separate CLI tools that work similarly:
- knife: The main CLI (comprehensive tool for listing and inspecting Go package objects)
- cutter: A lightweight tool focusing on listing type information
- typels: Lists types in a package
- objls: Lists objects in a package
- hagane: A template-based code generator
go install github.com/gostaticanalysis/knife/cmd/knife@latestgo install github.com/gostaticanalysis/knife/cmd/cutter@latestgo install github.com/gostaticanalysis/knife/cmd/typels@latestgo install github.com/gostaticanalysis/knife/cmd/objls@latestgo install github.com/gostaticanalysis/knife/cmd/hagane@latestknife works similarly to go list: You can specify a Go template using the -f option to customize the output.
For example:
knife -f "{{.}}" fmtFor more details, see Options.
Within the template specified by the -f flag, you can use various helper functions alongside the standard Go text/template package. For instance:
exported: Filters for exported items onlyidentical: Checks if two types are identicalimplements: Checks if a type implements a given interfacetypeof: Returns the type by namepos: Retrieves the position (file and line) of a definitionbr: Inserts a line break in the output
See the full list of available functions in Functions for a template.
Below are some common examples:
-
List functions in the
fmtpackage whose names begin withPrint:knife -f "{{range exported .Funcs}}{{.Name}}{{br}}{{end}}" fmt | grep Print Print Printf Println
-
List exported types in the
fmtpackage:knife -f "{{range exported .Types}}{{.Name}}{{br}}{{end}}" fmt Formatter GoStringer ScanState Scanner State Stringer -
List functions in
net/httpwhose first parameter iscontext.Context:knife -f '{{range exported .Funcs}}{{.Name}} {{with .Signature.Params}}{{index . 0}}{{end}}{{br}}{{end}}' net/http | grep context.Context NewRequestWithContext var ctx context.Context
-
List variables in
net/httpthat implement theerrorinterface:knife -f '{{range exported .Vars}}{{if implements . (typeof "error")}}{{.Name}}{{br}}{{end}}{{end}}' net/http ErrAbortHandler ErrBodyNotAllowed ErrBodyReadAfterClose ... ErrWriteAfterFlush -
List the position of fields whose type is
context.Context:knife -f '{{range .Types}}{{$t := .}}{{with struct .}}{{range .Fields}}{{if identical . (typeof "context.Context")}}{{$t.Name}} - {{pos .}}{{br}}{{end}}{{end}}{{end}}{{end}}' net/http Request - /usr/local/go/src/net/http/request.go:319:2 http2ServeConnOpts - /usr/local/go/src/net/http/h2_bundle.go:3878:2 ... -
Use an XPath expression to list AST node types (e.g.,
FuncDeclnames starting withPrint):knife -f '{{range .}}{{.Name}}:{{with .Scope}}{{.Names}}{{end}}{{br}}{{end}}' -xpath '//*[@type="FuncDecl"]/Name[starts-with(@Name, "Print")]' fmt Printf:[a err format n] Print:[a err n] Println:[a err n]
Both knife and cutter can run as Model Context Protocol (MCP) servers, enabling remote execution of Go static analysis operations through MCP-compatible clients like Claude Desktop.
To start knife as an MCP server:
knife mcpTo start cutter as an MCP server:
cutter mcpWhen using the MCP tools, you can provide the following parameters:
patterns(required): Package patterns to analyze (e.g.,["fmt", "net/http", "./..."])format(optional): Template string for output formatting (defaults to"{{.}}")data(optional): Extra data as key:value pairs (e.g.,"key1:value1,key2:value2")xpath(optional, knife only): XPath expression for AST node filtering
When connected to an MCP client, you can analyze Go packages remotely:
{
"patterns": ["fmt"],
"format": "{{range exported .Types}}{{.Name}}{{br}}{{end}}"
}This enables integration with AI assistants and other tools that support the Model Context Protocol.
cutter is a simplified version of knife that focuses on listing types.
Usage is almost identical to knife:
cutter -f "{{range exported .Funcs}}{{.Name}}{{br}}{{end}}" fmt | grep Print
Print
Printf
Printlntypels lists types in a package:
typels -implements io.Writer -kind interface io
io.Writer
io.ReadWriteCloser
io.WriteSeeker
io.ReadWriteSeeker
io.ReadWriter
io.WriteCloserobjls lists objects in a package:
objls -f const net/http | grep Status | head -5
net/http.StatusBadGateway
net/http.StatusMovedPermanently
net/http.StatusNotFound
net/http.StatusCreated
net/http.StatusForbiddenhagane is a template-based code generator that can produce Go code based on a specified template and source file(s):
hagane -template template.go.tmpl -o sample_mock.go -data '{"type":"DB"}' sample.go-o: Output file path (defaults to stdout)-f: Template format (defaults to{{.}})-template: Template file (used if-fis not set)-data: Extra data (JSON) passed into the template
For a complete example, see this hagane sample.
This project is licensed under the MIT License.
Contributions are always welcome! Feel free to open issues or PRs for bugs and enhancements.