How do I write inline documentation for methods and variables in Go code?

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

How do I write inline documentation for methods and variables in Go code?

问题

在Go语言中,通常使用注释来记录方法和变量,而不像PHP那样使用特定的注释标签。你可以在方法的上方使用//注释来描述方法的功能和参数,例如:

// SomeMethod 将参数b增加10个点
// 参数:b int
// 返回值:int
func SomeMethod(b int) int {
    return b + 10
}

这样的注释可以提供方法的说明和参数的类型信息,但并不是强制要求。在Go语言中,注释的内容更多地是为了提供文档和代码的可读性,而不是作为编译器的一部分。

英文:

is there some rules to document methods and variables in GO language?

for example on php

/**
 This method will increase parameter $b in 10 points
 @var int $b
 @return int

*/
public function someMethod($b){
    return $b+10;
}

is there something like that on GO, or there I must use only "// comment" above method without any @var or @return ?

答案1

得分: 12

你应该使用标准的//注释,因为这是官方文档工具godoc用来生成Go代码文档的方式。你可以查看官方Golang博客上的这篇文章了解更多信息:http://blog.golang.org/godoc-documenting-go-code

我还发现了这个很有趣:https://godoc.org/github.com/natefinch/godocgo

英文:

You should use standard // comments because this is what the official documentation tool called godoc will use to generate documentation for your go code. You can have a look at this post from the official golang blog about it: http://blog.golang.org/godoc-documenting-go-code

I also found this quite interesting: https://godoc.org/github.com/natefinch/godocgo

答案2

得分: 0

你应该使用标准的 // 注释,并且第一个单词必须是函数包的名称(用于生成器)。

Golang文档:

注意,这个注释是一个完整的句子,以描述的元素名称开头。这个重要的约定允许我们生成文档。

英文:

You should use standard // and the first word must be the name of function package (for generator) .

golang doc :

> Notice this comment is a complete sentence that begins with the name
> of the element it describes. This important convention allows us to
> generate documentation

huangapple
  • 本文由 发表于 2014年4月24日 20:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/23268729.html
匿名

发表评论

匿名网友

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

确定