]> git.feebdaed.xyz Git - 0xmirror/go.git/commitdiff
go/doc: reuse import name logic for examples
authorSean Liao <sean@liao.dev>
Mon, 8 Dec 2025 20:48:09 +0000 (20:48 +0000)
committerDavid Chase <drchase@google.com>
Fri, 12 Dec 2025 17:04:46 +0000 (09:04 -0800)
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 <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
src/go/doc/example.go
src/go/doc/testdata/examples/major_version.go [new file with mode: 0644]
src/go/doc/testdata/examples/major_version.golden [new file with mode: 0644]

index 228bdf58f81949ae9ae2f624690fa45047d42ea1..ba1f863df0a2858ac8bed64c8f7c94de4a311261 100644 (file)
@@ -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 (file)
index 0000000..6d91874
--- /dev/null
@@ -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 (file)
index 0000000..7cf2f2f
--- /dev/null
@@ -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