无法 运行 编译 go 文件

Cannot run compiled go files

我刚刚尝试按照官方文档安装 Go:https://golang.org/doc/install#install

但是,我在测试安装的时候遇到了问题。

我像文档中的示例一样创建了 hello.go 文件。 我可以 go build hello.go 成功,但我不能 运行 编译后的二进制文件。 而且,我可以go run hello.go.

heyuan@pop-os:~/Other/temp$ cat hello.go 
package main

import "fmt"

func main() {
        fmt.Printf("hello, world\n")
}
heyuan@pop-os:~/Other/temp$ go build hello.go 
heyuan@pop-os:~/Other/temp$ ls
hello  hello.go
heyuan@pop-os:~/Other/temp$ hello

Command 'hello' not found, but can be installed with:

sudo snap install hello              # version 2.10, or
sudo apt  install hello            
sudo apt  install hello-traditional

See 'snap info hello' for additional versions.

heyuan@pop-os:~/Other/temp$ go run hello.go 
hello, world

这是我的环境变量

export GOROOT=/usr/local/go
export GOPATH=/home/heyuan/Software/go
export PATH=${GOROOT}/bin:${PATH}:${GOPATH}/bin

bash 中,您可以 运行 将可执行文件 ./ 放在文件之前。

在你的情况下你必须这样做:

./hello

如果可执行文件在另一个文件夹中,则需要指定路径:

/opt/SP/executable/hello