goapi prints the exported API of a Go package as Go source without
implementations, in the style commonly used in Go proposals (see e.g.
golang/go#77626).
go install github.com/filippo-claude/goapi@latest
goapi [package]
The package argument defaults to .. It is resolved like any other go command
package argument (a relative path, or an import path in the current module or
workspace). It may also carry an explicit version, as in
example.com/foo@v1.2.3, in which case the module is downloaded on demand.
Constant declarations are printed with their values; variable declarations are printed with their types but without their initializers; functions and methods are printed without their bodies.
$ goapi container/ring
// Package ring implements operations on circular lists.
package ring
// A Ring is an element of a circular list, or ring.
// ...
type Ring struct {
Value any // for use by client; untouched by this library
// contains filtered or unexported fields
}
// New creates a ring of n elements.
func New(n int) *Ring
// Do calls function f on each element of the ring, in forward order.
// The behavior of Do is undefined if f changes *r.
func (r *Ring) Do(f func(any))
The package is loaded with golang.org/x/tools/go/packages (so full type
information is available, including for variable types inferred from
initializers). go/doc provides the exported-only API model — filtered fields
and methods, extracted doc comments, and the contains filtered or unexported fields markers. The declarations are then re-emitted as a Go source file with
bodies and initializers stripped, and run through go/format.