如何检查 Go 应用程序是否可构建?

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

How to check if a go app is buildable?

问题

我编写了一些代码,然后将应用程序构建到一个输出文件中,但有时我只想检查应用程序是否可构建,即是否没有错误并生成了编译后的输出,但实际上并不写入输出文件。

我尝试了这个变体,似乎可以工作:

go build -o /dev/null myapp

但我怀疑是否有更加“官方”的Go方法来检查是否可以构建。

请给予建议!

英文:

I write some code then build the app to an output file, but sometimes I just want to check if the app is buildable, i.e. has no errors and produces a compiled output, but without actually writing the output file.

I tried this variant and it seemed to work:

go build -o /dev/null myapp

But I suspect there must be a more "official" Go way to check if it can build.

Please advise!

答案1

得分: 2

要检查一个包或应用程序是否可构建,go build 是“官方”的方法。

你所做的是最简单的方法。在我看来,你应该坚持使用它。或者你可以这样做:

go build -o delme && rm delme

但是这种方法稍微慢一些,因为它必须写入结果,然后再删除,但这个解决方案是平台无关的(因为在Windows上不存在 /dev/null)。

当构建一个命令(main 包)时,根据定义,go build 将在_当前工作目录_中创建并保留结果。如果构建一个“普通”包(非 main),结果将被_丢弃_。详细信息请参阅:https://stackoverflow.com/questions/30612611/what-does-go-build-build

所以,如果你不喜欢使用 -o /dev/null 参数或手动删除结果,你可以将你的 main 包“转换”为一个非 main 包,我们称之为 main2。然后添加一个新的 main 包,除了导入和调用 main2.Main() 外,不做任何其他操作。构建 main2 包将不会留下任何文件。

例如,myapp/main.go

package main

import "myapp/main2"

func main() { main2.Main() }

myapp/main2/main2.go

// 原来在 main.go 中的所有内容

package main2

func Main() {
    // ...
}
英文:

To check if a package or app is buildable, go build is the "official" way.

What you did is the easiest way. In my opinion, you should stick to it. Alternatively you may do:

go build -o delme && rm delme

But it's somewhat slower as it has to write the result which is then deleted, but this solution is platform independent (as /dev/null does not exist on windows).

When building a command (main package), by definition go build will create and leave the result in the current working directory. If you build a "normal" package (non-main), the results will be discarded. See details here: https://stackoverflow.com/questions/30612611/what-does-go-build-build

So if it bothers you that you have to use the -o /dev/null param or manually delete the result, you may "transform" your main package to a non-main, let's call it main2. And add a new main package which should do nothing else but import and call main2.Main(). Building the main2 package will not leave any files behind.

E.g. myapp/main.go:

package main

import "myapp/main2"

func main() { main2.Main() }

myapp/main2/main2.go:

// Every content you originally had in main.go

package main2

func Main() {
    // ...
}

答案2

得分: 1

如何检查Go应用程序是否可构建?

根据我理解,您想要查看您正在编辑的文件是否没有错误。

您可以使用vim的vim-go插件

然后设置您的.vimrc文件以创建快捷方式:

"vim-go的快捷方式
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)

当我想要查看文件是否有错误时,我会在日常工作中使用这个快捷方式\+b,然后它会在不在终端中输入go build的情况下输出错误信息。

希望对您有所帮助。

英文:

> How to check if a go app is buildable?

As I understand your question, you wanted to see if the file you're editing has no error.

then you can use vim-go plugin for vim.

And then setup your .vimrc for creating the shortcut :

&quot;shortcut for vim-go
au FileType go nmap &lt;leader&gt;r &lt;Plug&gt;(go-run)
au FileType go nmap &lt;leader&gt;b &lt;Plug&gt;(go-build)
au FileType go nmap &lt;leader&gt;t &lt;Plug&gt;(go-test)
au FileType go nmap &lt;leader&gt;c &lt;Plug&gt;(go-coverage)

I use this as my daily work life when I want to see my file has no error I just type \+b. and then it will output the error without typing go build in the terminal.

hope it helps.

答案3

得分: 0

一种方法是对所有文件使用gofmt -e my_file.go,但是:

  • 这种方法不太适用于大规模的项目
  • 这实际上并不能报告go build可能出现的所有错误。

意思是:go build -o /dev/null可能是更彻底的方法。

如果只是验证语法和结构:可以参考gotype

英文:

One way would be gofmt -e my_file.go on all files, but:

  • that would not scale well
  • This doesn't actually report all errors that go build might.

Meaning: go build -o /dev/null might still be the more thorough approach.

For just validating syntax and structure: see gotype

答案4

得分: 0

我使用VSCode,发现它在使用这个扩展时比Sublime更好。当我保存文档时,它会对其进行格式化。在编写代码时,它会显示错误,并借助golint来帮助开发,使得开发变得非常可管理。

你也可以像使用gofmt一样单独使用golint。
你也可以尝试使用go vet。目前有关于使用哪个工具的争论,但我建议如果可以的话两个都使用。

英文:

I use VSCode and I've found it to be the better than Sublime when it comes to Go with this extension.
When I save the document, it formats it well. It shows the the errors as I write code with the help of golint making development very manageable.

You can also use golint separately just like gofmt.
You can also try go vet. There are debates going on around what to use when but I'd just advise to use both if you can.

huangapple
  • 本文由 发表于 2017年1月10日 13:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/41561819.html
匿名

发表评论

匿名网友

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

确定