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 <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Sean Liao 2025-12-08 20:48:09 +00:00 committed by David Chase
parent 927c89bbc5
commit e8a83788a4
3 changed files with 30 additions and 2 deletions

View 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 {

View File

@ -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
}

View File

@ -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