“go generate”会改变目录来运行脚本吗?

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

Will "go generate" change to the directory to run script?

问题

我正在尝试使用几个工具生成一些代码。所以,我写了一个名为data.go的文件,内容如下:

package main

//go:generate awk -v OFILE=names.go -f gen_table.awk $HOME/prj/header.h
//go:generate gofmt -w names.go
//go:generate ./gen_index_html.sh

生成的names.go文件不会自动格式化,所以我添加了第二行代码来强制进行正确的格式化。我假设它们会按顺序运行。

在运行这些命令之前,Go generate会跳转到这个目录吗?因为脚本gen_index_html.sh只能在当前目录中工作。

英文:

I'm trying to generate some code by several tools. So, I've write a file data.go as

package main           

//go:generate awk -v OFILE=names.go -f gen_table.awk $HOME/prj/header.h     
//go:generate gofmt -w names.go             
//go:generate ./gen_index_html.sh              

The generated names.go won't formatted automatically, so, I added the second line to force the correct format. And I assume it runs subsequently.
Will Go generate jump into this directory before run these command? Because the script gen_index_html.sh just accept work in current directory.

答案1

得分: 9

根据文档

生成器在包的源代码目录中运行。

因此,无论使用generate命令运行什么命令,都将在包含generate指令的文件所在的目录中运行。

一个文件中的多个generate指令按照源代码顺序依次执行。

英文:

Per the documentation:

> The generator is run in the package's source directory.

So, whatever command is run with generate will be run in the same directory as the file containing the generate directive.

Multiple generate directives in one file are executed one at a time in source code order.

huangapple
  • 本文由 发表于 2017年6月22日 17:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/44695539.html
匿名

发表评论

匿名网友

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

确定