dslice
dslice computes a static program slice of Go source code. Given a set
of root symbols (functions, types, methods, constants, or variables),
it extracts the minimal syntactic subset of the program required to
compile those roots.
Dependencies on internal packages are resolved recursively and inlined
into the output.
Example
$ dslice bytes.Equal
package bytes
// Equal reports whether a and b
// are the same length and contain the same bytes.
// A nil argument is equivalent to an empty slice.
func Equal(a, b []byte) bool {
return string(a) == string(b)
}
Install
go install git.sr.ht/~bwsd0/dslice@latest
Usage
dslice [flags] <symbol>
Flags:
-o string output file (default: stdout)
-name string output package name (default: inferred)
-private string comma-separated glob patterns of private module paths (sets GOPRIVATE)
Symbols are fully qualified: package/path.Symbol or
package/path.Type.Method.
$ dslice fmt.Println
$ dslice strings.Builder.Cap
$ dslice golang.org/x/tools/go/packages.Load
$ dslice -o slice.go encoding/json.Marshal
External and private modules
When the target symbol belongs to a module that is not a dependency
of the current working directory's go.mod, dslice automatically
fetches it into a temporary module workspace:
$ dslice github.com/pkg/errors.New
For private modules that are not available on the public module proxy,
use the -private flag to bypass the proxy and checksum database:
$ dslice -private 'github.com/corp/*' github.com/corp/lib.Func
This sets GOPRIVATE, GONOPROXY, and GONOSUMDB for all Go
toolchain invocations, including the automatic module fetch.
Multiple patterns are comma-separated, using the same syntax as
GOPRIVATE:
$ dslice -private 'github.com/corp/*,github.com/internal/*' github.com/corp/auth.Verify
Limitations
Static analysis only. No guarantees for reflection, interface
dispatch, or plugins.
References
M. Weiser, "Program Slicing," Proceedings of the 5th International
Conference on Software Engineering, pp. 439-449, IEEE Press, 1981.
https://dl.acm.org/doi/10.5555/800078.802557