I am trying to write a simple script in Go but get bad interpreter: Permission denied error

huangapple go评论85阅读模式
英文:

I am trying to write a simple script in Go but get bad interpreter: Permission denied error

问题

我正在尝试用Go语言编写一个脚本,但是我遇到了这个错误:

bad interpreter: Permission denied

我的超级简单的脚本如下:

#!/usr/local/Cellar/go/1.0.2/bin  
fmt.Println("Hello World")  

我不知道是否可能,但我真的很想用Go语言编写脚本,因为我非常喜欢这门语言。

英文:

I am trying to write a script in Go but I get this error:

>bad interpreter: Permission denied

My super simple script is as follow:

#!/usr/local/Cellar/go/1.0.2/bin  
fmt.Println("Hello World")  

I don´t know if this is possible but I would really like to write scripts in Go since I like the language a lot.

答案1

得分: 2

Go不是一种脚本语言。就像在C语言中一样,你必须编译源代码以生成可执行文件。

从“入门”中:

创建一个名为hello.go的文件,并将以下程序放入其中:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

然后使用go工具运行它:

$ go run hello.go
hello, world

为了与Python保持一致,有人试图使Go脚本成为可能。例如,你可以使用gorun做以下操作:

#!/usr/bin/gorun

package main

func main() {
    println("Hello world!")
}

但这并不是Go的真正逻辑,也不像你在问题中输入的那么简单。

英文:

Go isn't a scripting language. Like in C you have to compile your source code to make an executable.

From the "Getting Started" :

> Create a file named hello.go and put the following program in it:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

> Then run it with the go tool:

$ go run hello.go
hello, world

In the spirit of Python, there are attempts to make Go scripts kinda possible. Here's for example what you can do with gorun :

#!/usr/bin/gorun

package main

func main() {
    println("Hello world!")
}

But that's not really the logic of Go and that's not nearly as simple as what you typed in your question.

huangapple
  • 本文由 发表于 2012年9月12日 01:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/12374718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定