【Go】-bash: /usr/local/go/bin/go: cannot execute binary file

深度链接 / 2023-12-06 21:48:19 / 102

Go安装后执行写的第一个Go程序时出错-bash: /usr/local/go/bin/go: cannot execute binary file

1、下载

从官网https://golang.org/dl/或者中文网https://studygolang.com/dl下载,根据自己的操作系统下载相应的版本。Go语言支持Window、Linux、Mac OS X、FreeBSD等操作系统。下载是注意系统是32为还是64位的。

注意 :下载时一定要注意系统类型及系统是32位还是64位,不然执行会出问题。我的是32位的,安装了64位就出现下面问题。

[root@wrx local]# go
-bash: /usr/local/go/bin/go: cannot execute binary file
[root@wrx local]# uname
Linux
[root@wrx local]# cat /proc/version 
Linux version 2.6.32-642.13.1.el6.i686 (mockbuild@c1bm.rdu2.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Wed Jan 11 20:21:04 UTC 2017
[root@wrx local]#

2、安装

下载跟自己系统相对应的版本,解压到/usr/local/目录下

#下载
[root@wrx local]# wget https://studygolang.com/dl/golang/go1.9.linux-386.tar.gz
#解压至/usr/local目录下
[root@wrx local]# tar -C /usr/local -zxvf go1.9.linux-386.tar.gz 
#将 /usr/local/go/bin 目录添加至PATH环境变量
[root@wrx local]# export PATH=$PATH:/usr/local/go/bin
#查看是否安装成功
[root@wrx local]# go version
go version go1.9 linux/386
[root@wrx local]# go
Go is a tool for managing Go source code.

Usage:

	go command [arguments]

The commands are:

	build       compile packages and dependencies
	clean       remove object files
	doc         show documentation for package or symbol
	env         print Go environment information
	bug         start a bug report
	fix         run go tool fix on packages
	fmt         run gofmt on package sources
	generate    generate Go files by processing source
	get         download and install packages and dependencies
	install     compile and install packages and dependencies
	list        list packages
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

	c           calling between Go and C
	buildmode   description of build modes
	filetype    file types
	gopath      GOPATH environment variable
	environment environment variables
	importpath  import path syntax
	packages    description of package lists
	testflag    description of testing flags
	testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

3、第一个Go程序

进入/usr/local/go/src目录下,创建一个hello.go文件

[root@wrx local]# vim hello.go
#hello.go文件内容如下
package main

import "fmt"

func main() {
   fmt.Println("Hello World!")
}
#执行
[root@wrx local]# go run hello.go 
Hello World!