go runscriptsgo runscriptinggo run

更新:另外看到 gorun

gorun-Go源文件的类似脚本的运行器.

gorun - Script-like runner for Go source files.

尽管有一些动机和工具试图解决无法以脚本形式运行的问题,但我不会这么做,+有人告诉我它还没有准备好生产,并且建议不要在生产中使用它,因为尚未准备好生产,这是出于其目的,需要转而使用脚本便利性,而转而使用Go.(我没有反对编译,静态类型的任何东西,我非常喜欢它,但是想要类似于脚本编写便利性的东西.)

Though there is a motivation and tools which tried to workaround not being able to run as script, I will not go for it, +I've been told it's not production ready and was advised not to use it in production because it's not production ready, it's out of it's purpose and would need dump the scripting convenience in favour of Go. (I don't have anything against compiling, static typing, I'm a big fan of it, but wanted something similar to scripting convenience).

推荐答案

我不是专家,但是您可以执行以下操作.我知道这并不优雅,但也许可以从:)

I am not an expert in go, but you can do something like this. I know this is not elegant, but may be something to start with :)

~$ cat vikas.go
//usr/bin/env go run "$0" "$@"; exit

package main

import "fmt"

func main() {
    fmt.Printf("Hello World\n")
}
~$

~$ ./vikas.go
Hello World
~$

这篇关于是否可以将Go代码作为脚本运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!