Skip to content

proposal: cmd/go: add -test to go doc #79204

Description

@thatnealpatel

Proposal Details

In #17509, @rsc rejected this exact proposal:

Godoc is about showing the exported API from
a package. Test files are not part of the
exported API.

go doc has since greatly expanded in both its
intended scope alongside its unintended consumers,
namely large language models. The evolution of
go doc convinces me it's worth revisiting:

Merged CL Change
2015 https://go.dev/cl/9227 go doc
2015 https://go.dev/cl/11953 -cmd
2018 https://go.dev/cl/140959 -src
2018 https://go.dev/cl/141977 -all
2019 https://go.dev/cl/184017 -short
2025 https://go.dev/cl/628175 -http (#68106)
2026 https://go.dev/cl/445116 -ex (#26715)
open https://go.dev/cl/334876 -json (#34293)

go doc provides deterministic access to
information that is otherwise a few tool
calls, potentially with network round trips
as well. For LLM agents, it also provides
more token efficient output.

While I cannot prove this conjecture to be
true, I propose that we add a -test flag
to go doc.

Proposal

go doc -test [package] [symbol]

When specified, include symbols from *_test.go.
Both *_test packages and non-test packages with
testing symbols are in scope.

It composes with existing flags:

  • -test -src: full source of test declarations
  • -test -u: unexported test-file symbols
  • -test -all: test symbols in the full listing

Without -test, the go doc behavior is unchanged.

Examples

% go doc -test crypto/mlkem
package mlkem // import "crypto/mlkem"

Package mlkem implements the quantum-resistant key encapsulation method ML-KEM
(formerly known as Kyber), as specified in NIST FIPS 203.

Most applications should use the ML-KEM-768 parameter set, as implemented by
DecapsulationKey768 and EncapsulationKey768.

[NIST FIPS 203]: https://doi.org/10.6028/NIST.FIPS.203

const SharedKeySize = 32 ...
type DecapsulationKey1024 struct{ ... }
    func GenerateKey1024() (*DecapsulationKey1024, error)
    func NewDecapsulationKey1024(seed []byte) (*DecapsulationKey1024, error)
type DecapsulationKey768 struct{ ... }
    func GenerateKey768() (*DecapsulationKey768, error)
    func NewDecapsulationKey768(seed []byte) (*DecapsulationKey768, error)
type EncapsulationKey1024 struct{ ... }
    func NewEncapsulationKey1024(encapsulationKey []byte) (*EncapsulationKey1024, error)
type EncapsulationKey768 struct{ ... }
    func NewEncapsulationKey768(encapsulationKey []byte) (*EncapsulationKey768, error)

package mlkem_test // external test package

func BenchmarkDecaps(b *testing.B)
func BenchmarkEncaps(b *testing.B)
func BenchmarkKeyGen(b *testing.B)
func BenchmarkRoundTrip(b *testing.B)
func Bob(encapsulationKey []byte) (ciphertext []byte)
func Example()
func TestAccumulated(t *testing.T)
func TestBadLengths(t *testing.T)
func TestConstantSizes(t *testing.T)
func TestRoundTrip(t *testing.T)
% go doc -src -test crypto/mlkem BenchmarkKeyGen
package mlkem_test // external test package

func BenchmarkKeyGen(b *testing.B) {
        var d, z [32]byte
        rand.Read(d[:])
        rand.Read(z[:])
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                dk := mlkem.GenerateKeyInternal768(&d, &z)
                sink ^= dk.EncapsulationKey().Bytes()[0]
        }
}

/cc @golang/proposal-review

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoCommandcmd/goProposalToolProposalIssues describing a requested change to a Go tool or command-line program.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions