From: Sean Liao Date: Mon, 8 Dec 2025 20:48:09 +0000 (+0000) Subject: go/doc: reuse import name logic for examples X-Git-Tag: go1.26rc1~1^2~13 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e8a83788a4;p=gostls13.git go/doc: reuse import name logic for examples Examples resolved imports based on just the path name, but this doesn't work for go-name or modules on v2+. Updates #12794 Fixes #45110 Fixes #56740 Fixes #62059 Change-Id: I8c71b1e5311df04bccbdf319d759d3176a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/728280 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase --- diff --git a/src/go/doc/example.go b/src/go/doc/example.go index 228bdf58f8..ba1f863df0 100644 --- a/src/go/doc/example.go +++ b/src/go/doc/example.go @@ -11,7 +11,6 @@ import ( "go/ast" "go/token" "internal/lazyregexp" - "path" "slices" "strconv" "strings" @@ -221,7 +220,7 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File { // because the package syscall/js is not available in the playground. return nil } - n := path.Base(p) + n := assumedPackageName(p) if s.Name != nil { n = s.Name.Name switch n { diff --git a/src/go/doc/testdata/examples/major_version.go b/src/go/doc/testdata/examples/major_version.go new file mode 100644 index 0000000000..6d91874a89 --- /dev/null +++ b/src/go/doc/testdata/examples/major_version.go @@ -0,0 +1,14 @@ +package foo_test + +import ( + "example.com/foo/v3" + "example.com/go-bar" +) + +func Example() { + foo.Print("hello") + bar.Print("world") + // Output: + // hello + // world +} diff --git a/src/go/doc/testdata/examples/major_version.golden b/src/go/doc/testdata/examples/major_version.golden new file mode 100644 index 0000000000..7cf2f2f32a --- /dev/null +++ b/src/go/doc/testdata/examples/major_version.golden @@ -0,0 +1,15 @@ +-- .Play -- +package main + +import ( + "example.com/foo/v3" + "example.com/go-bar" +) + +func main() { + foo.Print("hello") + bar.Print("world") +} +-- .Output -- +hello +world