介绍

go generate 命令是go 1.4版本里面新添加的一个命令,当运行 go generate 时,它将扫描与当前包相关的源代码文件,找出所有包含 //go:generate 的特殊注释,提取并执行该特殊注释后面的命令,命令为可执行程序,形同shell下面执行。

有几点需要注意:

  • 该特殊注释必须在 .go 源码文件中。
  • 每个源码文件可以包含多个 generate 特殊注释时。
  • 显示运行 go generate 命令时,才会执行特殊注释后面的命令。
  • 命令串行执行的,如果出错,就终止后面的执行。
  • 特殊注释必须以 //go:generate 开头,双斜线后面没有空格。

应用

在有些场景下,我们会使用 go generate

  • yacc:从 .y 文件生成 .go 文件。
  • protobufs:从 protocol buffer 定义文件(.proto)生成 .pb.go 文件。
  • Unicode:从 UnicodeData.txt 生成 Unicode 表。
  • HTML:将 HTML 文件嵌入到 go 源码 。
  • bindata:将形如 JPEG 这样的文件转成 go 代码中的字节数组。

再比如:

  • string方法:为类似枚举常量这样的类型生成String()方法。
  • 宏:为既定的泛型包生成特定的实现,比如用于ints的sort.Ints。

命令

go generate 命令使用格式如下:

go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]

其中:

  • -run 正则表达式匹配命令行,仅执行匹配的命令
  • -v 输出被处理的包名和源文件名
  • -n 显示不执行命令
  • -x 显示并执行命令

执行 go generate 时,有一些环境变量可以使用:

$GOARCH
体系架构 (arm、amd64等待)
$GOOS
OS环境(linux、windows等)
$GOFILE
当前处理中的文件名
$GOLINE
当前命令在文件中的行号
$GOPACKAGE
当前处理文件的包名
$DOLLAR
固定的"$",不清楚用途

假设我们有个main.go文件,内容如下:

package main

import "fmt"

//go:generate echo hello
//go:generate go run main.go
//go:generate echo file=$GOFILE pkg=$GOPACKAGE
func main() {
fmt.Println("main func")
}

执行 go generate 后,输出如下:

$ go generate
hello
main func
file=main.go pkg=main

示例

现在我们来实践一下前面介绍的 go generate

String()方法

假设我们有一些代码,里面包含若干定义为Pill的整型常量:

package painkiller

type Pill int

const (
Placebo Pill = iota
Aspirin
Ibuprofen
Paracetamol
Acetaminophen = Paracetamol
)

为了调试的需要,我们会为这些常量定义String()签名方法:

func (p Pill) String() string

一般情况下,我们可能会像下面这样写:

func (p Pill) String() string {
switch p {
case Placebo:
return "Placebo"
case Aspirin:
return "Aspirin"
case Ibuprofen:
return "Ibuprofen"
case Paracetamol: // == Acetaminophen
return "Paracetamol"
}
return fmt.Sprintf("Pill(%d)", p)
}

这里,我们可以用 go generate 来实现String():

首先,我这里创建一个painkiller.go文件,包含如下内容:

//go:generate stringer -type=Pill
package painkiller type Pill int const (
Placebo Pill = iota
Aspirin
Ibuprofen
Paracetamol
Acetaminophen = Paracetamol
)

在文件的开头包含了一个 //go:generate stringer -type=Pill 特殊注释,其中stringer是个生成String方法的工具,为了使用stringer方法,在运行 go generate 命令前,我们需要安装stringer工具,命令如下:

$ go get golang.org/x/tools/cmd/stringer

然后,在 painkiller.go 所在的目录下面运行 go generate 命令:

$ go generate

我们会发现当前目录下面生成一个pill_string.go文件,里面实现了我们需要的String()方法,文件内容如下:

// Code generated by "stringer -type=Pill"; DO NOT EDIT.

package painkiller

import "fmt"

const _Pill_name = "PlaceboAspirinIbuprofenParacetamol"

var _Pill_index = [...]uint8{0, 7, 14, 23, 34}

func (i Pill) String() string {
if i < 0 || i >= Pill(len(_Pill_index)-1) {
return fmt.Sprintf("Pill(%d)", i)
}
return _Pill_name[_Pill_index[i]:_Pill_index[i+1]]
}

go generate的更多相关文章

  1. 使用mvn archetype:generate生产maven工程,响应很慢

    经常到下列时就不往下走了. 解决方案: 1.不使用interactive mode方式,直接指定DarchetypeArtifactId 2.仍使用interactive mode方式,但增加参数 - ...

  2. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  5. 在cmd命令行使用Maven Archetype插件 generate命令创建简单的java web项目

    前提: 1.下载apache-maven:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache ...

  6. No.022:Generate Parentheses

    问题: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  7. iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)

    从2月14日开始,上传程序的同学可能会遇到提示上传失败的提示. 并且打开自己的钥匙串,发现所有的证书全部都显示此证书签发者无效. 出现以下情况: Failed to locate or generat ...

  8. How to generate UML Diagrams from Java code in Eclipse

    UML diagrams compliment inline documentation ( javadoc ) and allow to better explore / understand a ...

  9. maven archetype:generate 命令简化项目模板数量

    在maven里使用 mvn archetype:generate 来创建项目是十分方便的,但有时也不尽然.在网络不好时,从网络上加载 archetype-catalog.xml文件(http://re ...

  10. Generate Time Data(普通日期主数据)

    Note: While using this option you need to replicate the standard table into SAP HANA that is T005T, ...

随机推荐

  1. 【网络安全】Linux基础详解

    声明:学习视频来自 b 站 up 主 泷羽 sec,如涉及侵权马上删除文章 声明:本文主要用作技术分享,所有内容仅供参考.任何使用或依赖于本文信息所造成的法律后果均与本人无关.请读者自行判断风险,并遵 ...

  2. [Symfony\Component\Process\Exception\RuntimeException] The Process class relies on proc_open, which is not available on your PHP installation.

    [Symfony\Component\Process\Exception\RuntimeException] The Process class relies on proc_open, which ...

  3. Qt编写视频监控系统(移动侦测/遮挡报警/区域入侵/越界侦测/报警输入输出等)

    一.前言 得益于标准的onvif协议,各大监控厂商的设备都会支持onvif协议,在onvif协议中就包括了事件订阅机制,通过这个机制,可以拿到各种报警事件,比如移动侦测/遮挡报警/区域入侵/越界侦测/ ...

  4. 《Bootstrap4Web设计与开发实战》源代码下载

    <Bootstrap4Web设计与开发实战>源代码下载: 链接:https://pan.baidu.com/s/1GaIo390c-l-gsT6-6RaaJA 提取码:fgiq 版权声明: ...

  5. 基于极坐标参数方程的直线Hough变换

  6. KMS for Windows 11

    I. 镜像下载 Windows 镜像下载地址:站点1,站点2 II. 手动激活 参考文档:Easy ways to activate Windows 11 for FREE without a pro ...

  7. 使用CRM REST Builder的Predefined Query在js结合FetchXML语句进行查询

    一般情况下使用拓展工具RESTBuilder编辑器,可以很方便的进行操作js中增删改查均能实现,但在某些较为特殊的场景下,需要根据条件去拼接查询过滤条件的,使用编辑器生成的代码无法实现,需要结合使用f ...

  8. LOL(英雄联盟) API 接口

    /*LOL(英雄联盟) API 接口 By wgscd /*LOL(英雄联盟) API 接口 By wgscd QQ:1009374598 */ GET https://127.0.0.1:58182 ...

  9. Java怎样实现将数据导出为Word文档

    文章首发于我的博客:Java怎样实现将数据导出为Word文档 - Liu Zijian's Blog 我们在开发一些系统的时候,例如OA系统,经常能遇到将审批单数据导出为word和excel文档的需求 ...

  10. 虚拟化基础vSphere