Go Doc出现了意外的缩进/分组函数。是什么原因导致的?

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

Go Doc is indenting/grouping functions unexpectedly. What is causing it?

问题

Go Doc在没有明确告诉它的情况下对代码进行了缩进/分组。

这是我浏览器上显示问题的截图。这四个Parse函数不应该缩进:

Go Doc出现了意外的缩进/分组函数。是什么原因导致的?

是什么导致了这种行为?

我尝试搜索Go Doc中的缩进/子节/分组,但除了功能请求之外,我没有找到任何东西。我相信我的问题的答案在文档中的某个地方,但由于我没有正确的词汇,所以找不到。

我对Go和Go Doc相对较新,所以我认为答案可能是我忽略了的简单问题。


这是我的代码摘录。如果需要分享更多代码,请告诉我。

status.go

package synop

// 解析块时返回的状态代码。
type Status int64

const (
	VALID Status = iota
	NO_DATA
    // 省略了其他代码
)

// 省略了其他函数

cloudwind_block.go

package synop

import (
	"strings"
)

/*
从云风块Cxxxx中提取云量。
云量C是块的第一个数字。云量以[okta]表示:
*/
func ParseCloud(block string) (okta int, s Status) {
	slice := [2]int{0, 1}
	return parseIfValid(block, slice, str2int)
}

/*
从云风块xDDxxx中提取风向。
方向DD是第二和第三个数字。
*/
func ParseDir(block string) (dir string, s Status) {
	slice := [2]int{1, 3}
	return parseIfValid(block, slice, getDir)
}

// 省略了其他函数

我还有另一个文件blocks.go,它的结构与status.go几乎相同,但它不会导致这种行为。我也不知道问题是由前面的类型Status还是cloudwind_block.go文件中的某些内容引起的。

我在单行文档中使用//,在多行文档中使用/* */。我尝试使其保持一致,但意料之中地没有效果。

英文:

Go Doc is indenting/making a group without me knowingly telling it to.

Here is a screenshot from my browser showing the problem. The four Parse functions should not be indented:

Go Doc出现了意外的缩进/分组函数。是什么原因导致的?

What is causing this behaviour?

I've tried searching for indents/subsections/grouping in Go Docs but I've not found anything beside feature requests. I'm sure the answer to my question is in the documentation somewhere but I can't find it as I don't have the correct vocabulary.

I'm relatively new to Go and Go Doc, so I'm assuming the answer is something simple which I've overlooked.


Here is an extract of my code. Please let me know if I need to share more code.

status.go

package synop

// Status codes returned when parsing Blocks.
type Status int64

const (
	VALID Status = iota
	NO_DATA
    // code omitted
)

// Other functions omitted

cloudwind_block.go

package synop

import (
	"strings"
)

/*
Extract cloud cover from the cloud-wind block, Cxxxx.
Cloud, C, is the first digit of the block. Cloud over is given in [okta]:
*/
func ParseCloud(block string) (okta int, s Status) {
	slice := [2]int{0, 1}
	return parseIfValid(block, slice, str2int)
}

/*
Extract wind direction from from the cloud-wind block, xDDxxx.
Direction, DD, are the second and third digits.
*/
func ParseDir(block string) (dir string, s Status) {
	slice := [2]int{1, 3}
	return parseIfValid(block, slice, getDir)
}

// Other functions omitted

I have another file, blocks.go, which has almost the same structure as status.go and it does not cause this behaviour. I also don't know if the problem is caused by the preceding type Status or something in the cloudwind_block.go file.

I'm using // for single-line documentation and /* */ for multi line. I've tried making this consistent on the off chance and, as expected, it had no effect.

答案1

得分: 3

分组和缩进的原因是这些函数被认为是它们所属类型的“构造函数”。

https://go.dev/doc/comment#func(如果你向下滚动一点,你会看到这个):

这个例子还显示了,*返回类型为 T 或指针 T 的顶级函数,可能还带有额外的错误结果,会与类型 T 及其方法一起显示,这是基于它们被假定为 T 的构造函数的假设。

英文:

The reason for the grouping and indentation is that those functions are considered "constructors" of the type under which they are grouped/indented.

https://go.dev/doc/comment#func (if you scroll down a bit, you'll see this):

>This example also shows that top-level functions returning a type T or pointer *T, perhaps with an additional error result, are shown alongside the type T and its methods, under the assumption that they are T’s constructors.

huangapple
  • 本文由 发表于 2022年11月25日 15:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/74569295.html
匿名

发表评论

匿名网友

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

确定