无法运行 go 脚本

时间:2021-05-25 00:41:13

标签: go

我有一个文件,我已经在其中写入了 go run 头文件,但该文件没有执行。

此站点显示了一个对我不起作用的示例。 https://coderwall.com/p/_kdjzq/go-run-run-go-as-a-script-language

./scripts/test.go

//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.

2 个答案:

答案 0 :(得分:3)

Go 文件不能是可执行文件,根据定义 [1]。

请不要试图这样对待它。只需运行go install [2],然后您就可以 从任何路径运行生成的程序。

  1. Should I use a Shebang with Bash scripts?
  2. https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies

答案 1 :(得分:0)

Go 是一种编译语言,而不是解释型语言。要正确运行 go 文件,首先要通过 go build <go file> 编译它。如果您想将 go 作为解释型语言运行,则可以使用名为 yaegi 的 go 解释器。