Can';不要运行go脚本
我有一个文件,我已经将go-run头写入其中,但该文件没有执行 这个网站展示了一个不适合我的例子 /脚本/test.goCan';不要运行go脚本,go,Go,我有一个文件,我已经将go-run头写入其中,但该文件没有执行 这个网站展示了一个不适合我的例子 /脚本/test.go //usr/bin/env go run "$0" "$@"; exit package scripts import ( "fmt" ) func main() { fmt.Println("hello") } 我试图这样调用它: chmod +x ./scripts/t
//usr/bin/env go run "$0" "$@"; exit
package scripts
import (
"fmt"
)
func main() {
fmt.Println("hello")
}
我试图这样调用它:
chmod +x ./scripts/test.go
./scripts/test.go
Failed to execute process './scripts/test.go'. Reason:
exec: Exec format error
The file './scripts/test.go' is marked as an executable but could not be run by the operating system.
根据定义,Go文件不能是可执行文件[1] 请不要这样对待它。只要运行
go install
[2],就可以了
从任何路径运行结果程序
Go是一种编译语言,而不是解释语言。要正确运行go文件,请首先通过
go build
编译它。如果您想将go作为解释语言运行,可以使用一个名为的go解释器。您希望它如何工作?您不能只获取任何语言源代码,然后像脚本一样运行它。你可以把这样的东西传给贝壳,但为什么?请参阅必须先编译Go,然后才能执行它。这是否回答了您的问题?安装后我该怎么做?