如果在常量定义中声明并使用了自定义类型,Godoc将不会生成”const”字段。

huangapple go评论74阅读模式
英文:

Godoc will not generate "const" field if custom type is declared and used in const definition?

问题

我发现Godoc是一个很好的自动生成文档的工具。但是我发现,如果我定义了一个自定义类型并在常量定义中使用它,在godoc的HTML中,常量将显示在该类型下方,而不是在包级别上方。

这是一个简单的例子:

const (
    Info = iota
    Warning
    Error
)

这将在godoc的顶部生成一个"Constants"标题。但是,如果我按照以下方式操作,包级别上将没有"Constants"标题:

type Level int
const (
    Info Level = iota
    Warning
    Error
)

在godoc输出中,常量将显示在type Level下方,位于文档的中间位置,而不是在顶部和包级别上方。

是否有办法在godoc中使用自定义类型,但仍将常量定义放在包级别上?

英文:

I find Godoc a great tool to automatically generate docs. But I found that, if I define a custom type and use it in my constant definition, in the godoc HTML, the constants will be displayed under that type, but not in the package level.

Here's a simple example:

const (
    Info = iota
    Warning
    Error
)

This will generate a "Constants" heading at the top of the godoc. But if I do the following, there will be no Constants heading for the package

type Level int
const (
    Info Level = iota
    Warning
    Error
)

In the godoc output, the constants will be displayed under type Level, somewhere in the middle of the document, but not at the top and not in the package level.

Is there any way to use custom types, but still put the const definitions in the package level in godoc?

答案1

得分: 1

GoDoc按类型进行分组。无法将有类型的常量的文档移到包级别。对于“工厂”函数、方法等也是如此。

英文:

GoDoc groups by type. It is not possible to move the documentation for typed constants to the package level. The same applies to "factory" functions, methods, etc.

答案2

得分: 1

将其按以下方式编写将使const块显示在包级别。我不确定这是否是预期行为还是只是一种不一致性。

type Level int
const (
    Info = Level(iota)
)
英文:

Writing it the following way will make the const block show up at package level. I’m not sure if this is intended behavior or just an inconsistency.

type Level int
const (
    Info = Level(iota)
)

huangapple
  • 本文由 发表于 2015年8月3日 22:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/31789348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定