Skip to content

Commit 9d90f93

Browse files
committed
go/doc: fix example generation for package prefixed with go-
Trim go- prefix from package name (like it's done on runtime) before looking for related unresolved package. Fixes #56740
1 parent 96711e4 commit 9d90f93

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/go/doc/example.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File {
238238
// We can't resolve dot imports (yet).
239239
return nil
240240
}
241+
} else {
242+
// Trim 'go-' prefix from package name (like done at runtime) to ensure match
243+
n = strings.TrimPrefix(n, "go-")
241244
}
242245
if unresolved[n] {
243246
// Copy the spec and its path to avoid modifying the original.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package foo_test
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/xxx/go-foo"
11+
)
12+
13+
func ExampleMyMethod() {
14+
fmt.Println(foo.MyMethod())
15+
// Output:
16+
// expected output
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- MyMethod.Play --
2+
package main
3+
4+
import (
5+
"fmt"
6+
7+
"github.com/xxx/go-foo"
8+
)
9+
10+
func main() {
11+
fmt.Println(foo.MyMethod())
12+
}
13+
-- MyMethod.Output --
14+
expected output

0 commit comments

Comments
 (0)