用标记包裹长'go:generate'行?

时间:2018-03-25 18:28:21

标签: go

是否可以在Golang中用标记包装长行?

以下是一个例子:

//go:generate mockgen -destination=mock/interface.go -package=mock -source=interface.go -mock_names=ConnPool=ConnPool,Row=Row,Rows=Rows,CommandTag=CommandTag,Conn=Conn
type ConnPool interface {
    ...    
}

2 个答案:

答案 0 :(得分:3)

The Go Programming Language Specification

Comments

Comments serve as program documentation. There are two forms:

1 Line comments start with the character sequence // and stop at the end of the line.

2 General comments start with the character sequence /* and stop with the first subsequent character sequence */.

A comment cannot start inside a rune or string literal, or inside a comment. A general comment containing no newlines acts like a space. Any other comment acts like a newline.


No. The //go:generate pragma is a special form of a line comment: "Line comments start with the character sequence // and stop at the end of the line."

答案 1 :(得分:0)

您可以尝试以下方式:

使用base64编码generate命令

base64 <<< 'mockgen -destination=mock/interface.go -package=mock -source=interface.go -mock_names=ConnPool=ConnPool,Row=Row,Rows=Rows,CommandTag=CommandTag,Conn=Conn'

将编码内容插入带有注释前缀的go文件的第一行

//bW9ja2dlbiAtZGVzdGluYXRpb249bW9jay9pbnRlcmZhY2UuZ28gLXBhY2thZ2U9bW9jayAtc291
//cmNlPWludGVyZmFjZS5nbyAtbW9ja19uYW1lcz1Db25uUG9vbD1Db25uUG9vbCxSb3c9Um93LFJv
//d3M9Um93cyxDb21tYW5kVGFnPUNvbW1hbmRUYWcsQ29ubj1Db25uCg==

然后按照base64内容添加解码并运行generate命令

//go:generate sh -c "eval $(head -3 $GOFILE | tr -d '^[ ]*//[ ]*' | base64 -d)"

上次运行go generate yourfile.go