英文:
Can't run go script
问题
我有一个文件,其中我已经写入了"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
无法执行进程'./scripts/test.go'。原因:
exec: 执行格式错误
文件'./scripts/test.go'被标记为可执行文件,但操作系统无法运行它。
英文:
I have a file to which I've written the go run header, but the file isn't executing.
This site shows an example that doesn't work for me. 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")
}
I am trying to invoke it like this:
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.
答案1
得分: 3
一个Go文件不能是可执行文件,根据定义[1]。
请不要试图将其视为可执行文件。只需运行go install
[2],然后您可以从任何路径运行生成的程序。
- https://stackoverflow.com/questions/25165808
- https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies
英文:
A Go file cannot be an executable file, by definition [1].
Please, do not try to treat it as such. Just run go install
[2], then you can
run the resultant program from any path.
- https://stackoverflow.com/questions/25165808
- <https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies>
答案2
得分: 0
Go是一种编译语言,而不是解释语言。要正确运行Go文件,首先通过go build <go文件>
进行编译。如果你想将Go作为解释语言运行,可以使用一个名为yaegi的Go解释器。
英文:
Go is a compiled language, not an interpreted one. To run a go file properly, first compile it via go build <go file>
. If you want to run go as an interpreted language, you can use a go interpreter called yaegi.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论