合适的 Go shebang 线路是什么?
我喜欢使用 shebangs 直接运行我的 Perl 脚本:
#!/usr/bin/env perl
Go 程序的 shebang 是什么?
I like using shebangs to run my Perl scripts directly:
#!/usr/bin/env perl
What's the shebang for Go programs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
//usr/bin/go run $0 $@ ; exit
示例:
将
//
视为单行注释并且 shell 忽略额外的
/
更新:Go 安装可能位于不同的位置。下面的语法将考虑到这一点,并且适用于 Mac:
//$GOROOT/bin/go run $0 $@ ;退出
//usr/bin/go run $0 $@ ; exit
example:
go treat
//
as a single line commentand shell ignore extra
/
Update: Go installation may be in a different location. The syntax below will take that into account, and works for Macs:
//$GOROOT/bin/go run $0 $@ ; exit
我更喜欢这个:
与 ه٥٩٠٠٠پپ 的答案相比,这有几个优点:
///usr/bin/true
实现了同时有效的 Go 和 shell 语法的壮举。在 Go 中它是一个注释。在shell中,它是无操作命令。使用
exec
替换新的 shell 进程,而不是启动孙进程。因此,您的 Go 程序将是一个直接子进程。这样效率更高,对于一些高级情况(例如调试和监控)也很重要。正确引用参数。空格和特殊字符不会导致问题。
领先的
///
比//
更符合标准。如果您只使用//
,您就会面临遇到实现定义的行为的风险。这是来自 http://pubs.opengroup.org/onlinepubs/9699919799/basedefs 的引用/V1_chap04.html:我已经用 bash、dash、zsh 和 ksh 测试了这个答案。
例子:
I prefer this:
This has several advantages compared to the answer by هومن جاویدپور:
The
///usr/bin/true
accomplishes the feat of simultaneously being valid Go and shell syntax. In Go it is a comment. In shell, it is no-op command.Uses
exec
to replace the new shell process instead of launching a grandchild process. As a result, your Go program will be a direct child process. This is more efficient and it's also important for some advanced situations, such as debugging and monitoring.Proper quoting of arguments. Spaces and special characters won't cause problems.
The leading
///
is more standards compliant than just//
. If you only use//
, you run the risk of bumping into implementation-defined behaviour. Here's a quote from http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html:I have tested this answer with bash, dash, zsh, and ksh.
Example:
默认情况下是没有的。不过,有一个名为 gorun 的第三方工具可以让您做到这一点。 https://wiki.ubuntu.com/gorun
不幸的是,编译器不喜欢 shebang 行。您无法编译使用 gorun 运行的相同代码。
There isn't one by default. There is a third-party tool called gorun that will allow you to do it, though. https://wiki.ubuntu.com/gorun
Unfortunately the compilers don't like the shebang line. You can't compile the same code you run with gorun.
Go程序被编译为二进制文件;我认为没有直接从源代码运行它们的选项。
这与其他编译语言(例如 C++ 或 Java)类似。某些语言(例如 Haskell)提供完全编译模式和“脚本”模式,您可以使用 shebang 行直接从源代码运行。
Go programs are compiled to binaries; I don't think there is an option to run them directly from source.
This is similar to other compiled languages such as C++ or Java. Some languages (such as Haskell) offer both a fully compiled mode and a "script" mode which you can run directly from source with a shebang line.
就我而言,没有 GOROOT 环境。所以我就这么做了。
In my case, there was no GOROOT env. So I did this.
到目前为止,事实证明,链接 sh 和 gorun 对我来说是最便携的解决方案。
输出:
So far, chaining sh and gorun has proven to be the most portable solution for me.
OUTPUT:
/// 2>/dev/null; gorun "$0" "$@" ; exit $?
在我看来,这是迄今为止答案的最佳组合。它使用 gorun 来缓存编译步骤(这会大大加快脚本速度),并且也可以在 macOS 上运行。
安装
gorun
:go get github.com/erning/gorun
/// 2>/dev/null; gorun "$0" "$@" ; exit $?
Seems to me the best combination of the answers thus far. It uses
gorun
so it caches the compilation step (which speeds up your scripts greatly), and works on macOS, too.Install
gorun
with:go get github.com/erning/gorun
阅读所有这些消息,这似乎是最便携的:
Reading all this messages this seems the most portable:
如果你真的想要一个可执行文件(不能从 shell 调用)并且不想需要 gorun,可能的解决方案可能是这样的:
if really you want an executable (callable not fro shells) and not wanting to require gorun a possible solution could be something like:
我的偏好是这样的:
您可以在我的个人 hello_world.go< 中看到这一点/a> 文件。如果我改变主意,我会更新该文件。
这样做是现在主要两个答案之间的混合(此处和此处)。
说明:
///usr/bin/env
与/usr/bin/env
相同。而且,在 Go 中,双斜杠//
是注释。因此,现在第一行既是有效的 Bash/shell 命令,又是有效的 Bash 注释。/usr/bin/env go
比/usr/bin/go
更好,因为您的go
可执行文件可能不位于/usr/bin/go
。例如,which go
对我来说显示我的位置位于/usr/local/go/bin/go
。env
程序可以帮助您找到正确的位置。hello_world.go
,那么直接以./hello_world.go
运行它将会调用go run "$0" "$@"
,它在此可执行文件上调用go run
,将其路径作为$0
传递,将所有其他参数作为$@
传递。exit
可确保 bash 在此时退出文件,并避免尝试将 Go 代码作为 shell 代码运行。这就像在第一行之后注释掉整个 Go(至少在你的 shell 中)。来自 hello_world.go:
示例运行命令和输出:
另请参阅
参考文献
//< /code> 和
exit
部分:Shebang 以//
开头?My preference is this:
You can see this in my personal hello_world.go file. If I ever change my mind, I'll update that file.
Doing it this way is a bit of a mix between the main two answers right now (here and here).
Explanation:
///usr/bin/env
is the same as/usr/bin/env
. And, in Go, a double slash//
is a comment. So, now the first line is both a valid Bash/shell command and a valid Bash comment./usr/bin/env go
is better than/usr/bin/go
because yourgo
executable may not be located at/usr/bin/go
.which go
for me shows mine is located at/usr/local/go/bin/go
, for example. Theenv
program helps find your proper location.hello_world.go
, then running it directly as./hello_world.go
will callgo run "$0" "$@"
, which callsgo run
on this executable, passing in the path to it as$0
and all other arguments as$@
.exit
at the end ensures that bash exits the file at that point and avoids trying to run your Go code as shell code. It's like commenting out the whole Go (to your shell at least) after the first line.From hello_world.go:
Sample run commands, and output:
See also
References
//
andexit
parts: Shebang starting with//
?