English 中文(简体)
链接到doc.go文件中另一个包的最佳方式
原标题:Best way to link to another package in doc.go files
  • 时间:2023-06-28 00:24:19
  •  标签:
  • go
  • godoc

在<code>doc.go</code>文件中编写包文档时,链接到另一个包中的文档的最佳方式是什么?不幸的是,引用导入包的常规方法在doc.go文件中不起作用,因为不允许使用未使用的导入。

// Package foo docs in a doc.go file
// foo uses [bar.Bar] types for doing things.
package foo

import "foo.com/jonathan/godoctest/bar" // Unused import error here

使用完全限定的路径确实有效,但不能获得最可读的文档:

// Package foo docs in a doc.go file
// foo uses [foo.com/jonathan/godoctest/bar.Bar] types for doing things.
package foo

有什么变通办法吗?

问题回答

使用名为_的变量引用导入包中的标识符(空白标识符

// Package foo docs in a doc.go file
// foo uses [bar.Bar] types for doing things.
package foo

import "foo.com/jonathan/godoctest/bar"

var _ bar.SomeType // where bar.SomeType is a type
var _ = bar.Value // where bar.Value is a func, var, constant, ...

只需要一个对导入包的引用。上面的代码显示了引用类型或值的不同方法。





相关问题
minimum work size of a goroutine [closed]

Does anyone know approximately what the minimum work size is needed in order for a goroutine to be beneficial (assuming that there are free cores for the work to be offloaded to)?

How do you get the terminal size in Go?

How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts); But how to i access TIOCGWINSZ in Go

What do you use to write Go [closed]

I know its a bit too early, but I ve been trying out Go (Google s Programming Language) and its kindof annoying to write code in gedit. So, my question: What do you use to experiment with Go?

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

热门标签