【编程语言之Go介绍与环境安装】

Go语言是谷歌2009发布的第二款开源编程语言。

Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。

Go特色

简洁 快速 安全

并行 有趣 开源,

内存管理,数组安全,编译迅速

Go语言具有以下的特征

自动垃圾回收 

更丰富的内置类型 

 函数多返回值 

 错误处理 

 匿名函数和闭包 

 类型和接口 

并发编程 

反射 

语言交互性

二、软件安装过程

[root@node1 ~]# file /bin/ls

/bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped

[root@node1 ~]# wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz

--12:10:22--  https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz

Resolving storage.googleapis.com... 172.217.24.16, 2404:6800:4008:800::2010

Connecting to storage.googleapis.com|172.217.24.16|:443... connected.

ERROR: certificate common name `*.storage.googleapis.com' doesn't match requested host name `storage.googleapis.com'.

To connect to storage.googleapis.com insecurely, use `--no-check-certificate'.

Unable to establish SSL connection.

[root@node1 ~]# wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz --no-check-certificate

--12:10:49--  https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz

Resolving storage.googleapis.com... 172.217.24.16, 2404:6800:4008:800::2010

Connecting to storage.googleapis.com|172.217.24.16|:443... connected.

WARNING: certificate common name `*.storage.googleapis.com' doesn't match requested host name `storage.googleapis.com'.

HTTP request sent, awaiting response... 200 OK

Length: 90029041 (86M) [application/x-gzip]

Saving to: `go1.8.3.linux-amd64.tar.gz'

100%[==================================================>] 90,029,041   341K/s   in 3m 34s 

12:14:33 (410 KB/s) - `go1.8.3.linux-amd64.tar.gz' saved [90029041/90029041]

[root@node1 ~]# ls

anaconda-ks.cfg      Desktop   go1.8.3.linux-amd64.tar.gz     jdk-7u67-linux-x64.rpm  sqlite-3.5.6.tar.gz

apache-flume-1.6.0-bin.tar.gz  elasticsearch-2.1.1.rpm     grafana-1.9.1.tar.gz           kafka_2.9.1-0.8.2.2.tgz   tomcat7 apache-tomcat-7.0.67.zip       elasticsearch-2.1.1.tar.gz  influxdb-0.9.4.2-1.x86_64.rpm  kibana-4.3.1-linux-x64.tar.gz  yum-3.2.29-30.el6.noarch.rpm

collectd-5.5.0     facette-0.3.0    install.log     logstash-2.1.1.tar.gz   yum-3.2.29-81.el6.centos.noarch.rpm

collectd-5.5.0.tar.gz          facette-0.3.0.tar.gz        install.log.syslog             sqlite-3.5.6

[root@node1 ~]# tar -zxvf go1.8.3.linux-amd64.tar.gz 

[root@node1 ~]# ll

total 404848

-rw-------  1 root   root        883 Nov 22  2015 anaconda-ks.cfg

-rw-r--r--  1 root   root   52550402 May 20  2015 apache-flume-1.6.0-bin.tar.gz

-rw-r--r--  1 root   root    9461749 Dec  7  2015 apache-tomcat-7.0.67.zip

drwxr-xr-x  6 131026  5000      4096 Jan 29  2016 collectd-5.5.0

-rw-r--r--  1 root   root    2244454 May 27  2015 collectd-5.5.0.tar.gz

drwxr-xr-x  2 root   root       4096 Jan 25  2016 Desktop

-rw-r--r--  1 root   root   28896574 Dec 17  2015 elasticsearch-2.1.1.rpm

-rw-r--r--  1 root   root   29007342 Dec 17  2015 elasticsearch-2.1.1.tar.gz

drwxrwxr-x  9 root   root       4096 Jan 29  2016 facette-0.3.0

-rw-r--r--  1 root   root    1040968 Jan 29  2016 facette-0.3.0.tar.gz

drwxr-xr-x 11 root   root       4096 May 24 11:16 go

-rw-r--r--  1 root   root   90029041 May 24 14:09 go1.8.3.linux-amd64.tar.gz

-rw-r--r--  1 root   root    1263568 Dec 29  2014 grafana-1.9.1.tar.gz

-rw-r--r--  1 root   root    8248423 Sep 29  2015 influxdb-0.9.4.2-1.x86_64.rpm

-rw-r--r--  1 root   root      35250 Nov 22  2015 install.log

-rw-r--r--  1 root   root       4264 Nov 22  2015 install.log.syslog

-rwxrwxr-x  1 root   root  126857158 Nov 24  2015 jdk-7u67-linux-x64.rpm

-rw-r--r--  1 root   root   17139880 Nov 24  2015 kafka_2.9.1-0.8.2.2.tgz

-rw-r--r--  1 root   root   30408272 Dec 16  2015 kibana-4.3.1-linux-x64.tar.gz

-rw-r--r--  1 root   root   12222033 Feb  1  2016 logstash-2.1.1.tar.gz

drwxr-xr-x 13   1000 users      4096 May 27 16:28 sqlite-3.5.6

-rw-r--r--  1 root   root    2543979 Jul 30  2016 sqlite-3.5.6.tar.gz

drwxr-xr-x  9 root   root       4096 Feb 26 13:14 tomcat7

-rw-r--r--  1 root   root    1016296 May 10  2012 yum-3.2.29-30.el6.noarch.rpm

-rw-r--r--  1 root   root    1042364 May 27 16:14 yum-3.2.29-81.el6.centos.noarch.rpm

[root@node1 ~]# rm -rf go1.8.3.linux-amd64.tar.gz

[root@node1 ~]# cd go/

[root@node1 go]# ls

api  AUTHORS  bin  blog  CONTRIBUTING.md  CONTRIBUTORS  doc  favicon.ico  lib  LICENSE  misc  PATENTS  pkg  README.md  robots.txt  src  test  VERSION

[root@node1 go]# cd bin/

[root@node1 bin]# ls

go  godoc  gofmt

[root@node1 bin]# pwd

/root/go/bin

[root@node1 bin]# vi ~/.bash_profile 

JAVA_HOME=/opt/es5.0/jdk1.8.0_111

HADOOP_HOME=/opt/hadoop

STORM_HOME=/opt/storm096

GO_HOME=/root/go

PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$HADOOP_HOME/bin:$STORM_HOME/bin:$GO_HOME/bin

export PATH  JAVA_HOME HADOOP_HOME STORM_HOME GO_HOME

[root@node1 bin]# source  ~/.bash_profile 

[root@node1 bin]# 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.

[root@node1 ~]# go env

GOARCH="amd64"

GOBIN=""

GOEXE=""

GOHOSTARCH="amd64"

GOHOSTOS="linux"

GOOS="linux"

GOPATH="/root/go"

GORACE=""

GOROOT="/usr/local/go"

GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"

GCCGO="gccgo"

CC="gcc"

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"

CXX="g++"

CGO_ENABLED="1"

PKG_CONFIG="pkg-config"

CGO_CFLAGS="-g -O2"

CGO_CPPFLAGS=""

CGO_CXXFLAGS="-g -O2"

CGO_FFLAGS="-g -O2"

CGO_LDFLAGS="-g -O2"

[root@node1 ~]# go build demo.go 

go: cannot find GOROOT directory: /usr/local/go/go

[root@node1 ~]# export GOROOT=/usr/local/go

[root@node1 ~]# export GOROOT=/usr/local/go/go

[root@node1 ~]# go build demo.go 

go: cannot find GOROOT directory: /usr/local/go/go

解释这是GO语言的一个小Bug:原来还需设置GOROOT这个属性 The default is hard-coded at compile time; in thelinux distribution it is set to /usr/local/go

[root@node1 ~]# export GOROOT=/usr/local/go

[root@node1 ~]# mv go/  /usr/local/

mv: cannot move `go/' to a subdirectory of itself, `/usr/local/go'

[root@node1 ~]# pwd

/root

[root@node1 ~]# rm -rf /usr/local/go/

[root@node1 ~]# mv -r go/  /usr/local/

mv: invalid option -- r

Try `mv --help' for more information.

[root@node1 ~]# cp  -r go/  /usr/local/

[root@node1 ~]# rm -rf  go/

解决方案:安装go语言在/usr/local/go目录下面

 

[root@node1 ~]# vi ~/.bash_profile 

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

# User specific environment and startup programs

JAVA_HOME=/opt/es5.0/jdk1.8.0_111

HADOOP_HOME=/opt/hadoop

STORM_HOME=/opt/storm096

GO_HOME=/usr/local/go

PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$HADOOP_HOME/bin:$STORM_HOME/bin:$GO_HOME/bin

export PATH  JAVA_HOME HADOOP_HOME STORM_HOME GO_HOME

unset USERNAME

[root@node1 ~]# vi demo.go 

package main

import "fmt"

func main() {

   fmt.Println("Hello, World!")

}

~

"demo.go" 6L, 75C written

[root@node1 ~]# go build demo.go 

[root@node1 ~]# ls

 influxdb-0.9.4.2-1.x86_64.rpm  kibana-4.3.1-linux-x64.tar.gz  yum-3.2.29-30.el6.noarch.rpm

collectd-5.5.0.tar.gz          elasticsearch-2.1.1.tar.gz  install.log                    logstash-2.1.1.tar.gz          yum-3.2.29-81.el6.centos.noarch.rpm

[root@node1 ~]# sh demo

demo: demo: cannot execute binary file

[root@node1 ~]# ./demo 

Hello, World!

[root@node1 ~]# go run demo.go 

Hello, World!

[root@node1 ~]# 



 

猜你喜欢

转载自gaojingsong.iteye.com/blog/2386821