热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

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

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

 

Go特色

简洁 快速 安全

并行 有趣 开源,

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

 

Go语言具有以下的特征

自动垃圾回收 

更丰富的内置类型 

 函数多返回值 

 错误处理 

 匿名函数和闭包 

 类型和接口 

并发编程 

反射 

语言交互性

 

二、软件安装过程

[[email protected] ~]# 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

[[email protected] ~]# 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.

[[email protected] ~]# 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]

 

[[email protected] ~]# 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

[[email protected] ~]# tar -zxvf go1.8.3.linux-amd64.tar.gz 

[[email protected] ~]# 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

[[email protected] ~]# rm -rf go1.8.3.linux-amd64.tar.gz

[[email protected] ~]# cd go/

[[email protected] go]# ls

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

[[email protected] go]# cd bin/

[[email protected] bin]# ls

go  godoc  gofmt

[[email protected] bin]# pwd

/root/go/bin

[[email protected] 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

 

 

[[email protected] bin]# source  ~/.bash_profile 

[[email protected] 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.

 

[[email protected] ~]# 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"

[[email protected] ~]# go build demo.go 

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

[[email protected] ~]# export GOROOT=/usr/local/go

 

[[email protected] ~]# export GOROOT=/usr/local/go/go

[[email protected] ~]# 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

[[email protected] ~]# export GOROOT=/usr/local/go

 

[[email protected] ~]# mv go/  /usr/local/

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

[[email protected] ~]# pwd

/root

[[email protected] ~]# rm -rf /usr/local/go/

[[email protected] ~]# mv -r go/  /usr/local/

mv: invalid option -- r

Try `mv --help' for more information.

[[email protected] ~]# cp  -r go/  /usr/local/

[[email protected] ~]# rm -rf  go/

解决方案:安装go语言在/usr/local/go目录下面
【编程语言之Go介绍与环境安装】
 

[[email protected] ~]# 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

 

 

[[email protected] ~]# vi demo.go 

package main

import "fmt"

 

func main() {

   fmt.Println("Hello, World!")

}

~

"demo.go" 6L, 75C written

[[email protected] ~]# go build demo.go 

[[email protected] ~]# 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

[[email protected] ~]# sh demo

demo: demo: cannot execute binary file

[[email protected] ~]# ./demo 

Hello, World!

[[email protected] ~]# go run demo.go 

Hello, World!

[[email protected] ~]# 


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


推荐阅读
author-avatar
2yuheng
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有