GO语言的进阶之路-初探GO语言

                           GO语言的进阶之路-初探GO语言

                                                  作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

一.为什么我们需要一门新语言

   Go语言官方自称,之所以开发Go 语言,是因为“近10年来开发程序之难让我们有点沮丧”。 这一定位暗示了Go语言希望取代C和Java的地位,成为最流行的通用开发语言。

   编程语言已经非常多,偏性能敏感的编译型语言有 C、C++、Java、C#、Delphi和Objective-C等,偏快速业务开发的动态解析型语言有PHP、Python、Perl、Ruby、JavaScript和Lua等,面向特定领域的语言有Erlang、R和MATLAB等,那么我们为什么需要 Go这样一门新语言呢?下面我们一起看一下近三年来GO语言在西方国家的使用情况,正杰只能说一路飙升,而且飙升的速度比python还要快!

     在2000年前的单机时代,C语言是编程之王。随着机器性能的提升、软件规模与复杂度的提高,Java逐步取代了C的位置。尽管看起来Java已经深获人心,但Java编程的体验并未尽如人意。历年来的编程语言排行榜(如图0-1所示)显示,Java语言的市场份额在逐步下跌,并趋近于C语言的水平,显示了这门语言后劲不足。

 

          2015年12月编程语言排行榜:http://www.csdn.net/article/2015-12-08/2826418-TIOBE(JAVA处于巅峰,此时的GO处于第五十名。)

         

             到2016年12月的时候编程语言排行榜飙升到16名:http://www.52bcx.com/BC/news/1225.html(在Top10榜单中,另一个引入注目的是则属Python,其份额在持续上升中,目前排名第四位,Python易于学习,而且借助Python还可进行全栈开发。)

             

        当然到2017的12月还没有到来,现在是3月,那么问题来了,这个GO语言真的能吃得消吗?会不会逆袭进阶前10呢?让我们拭目以待吧。

 

二.Windows环境下Golang的安装

  说起安装环境大家首先得去下载相应的安装包,给大家推荐2个地址去下载相应的版本,我比较推荐国内的地址,因为没有被墙掉,如果你有VPN的话那就大可不必,直接去国外的官网上下载就好:

  a>.https://dl.gocn.io/ (国内下载地址)

  b>.https://golang.org/dl/ (国外下载地址,需VPN)

  对了,如果不方便FQ的话(FQ可是运维的基本功哟!),可以用我下载好的地址:链接:http://pan.baidu.com/s/1pLmJF4n 密码:kdbf

1.选择相应的软件包,我这里选择的是压缩包,这样方便你指定安装的路径。

 

2.将下载后的软件解压到指定的目录,你可以自定义自己的路径。

3.设置Golang环境变量

  Go 语言需要配置 GOROOT 和 Path 两个环境变量:GOROOT 和 GOPATH。以及后期要用到的Go的工作目录环境GOPATH。

 

a>.根目录 GOROOT 和 Path设置

打开环境变量

 

 

新建GOROOT环境变量

编辑PATH环境变量

 

   这个是后续代码目录,新建系统变量 GOPATH,将其指向你的代码目录:

   打开 CMD 终端,输入 go env 如果出现以下界面就说明你的安装是成功了。

 

三.Linux环境下Golang的安装

1.选择相应的版本进行安装,这里我选择的是Linux的安装包。

 2.源码安装Golang环境

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# mkdir -p /yinzhengjie/golang/local  #创建go语言的虎安装环境目录
 9 [root@yinzhengjie ~]# cd /yinzhengjie/golang/local/
10 [root@yinzhengjie local]# yum -y install lrzsz  #安装linux和windows交换上传工具(这个工具默认是不支持上传4G的文件的哟)
11 [root@yinzhengjie local]# ls
12 go1.8.linux-amd64.tar.gz
13 [root@yinzhengjie local]# tar -xf go1.8.linux-amd64.tar.gz  #用tar解压这个压缩包
14 You have new mail in /var/spool/mail/root
15 [root@yinzhengjie local]# ls
16 go  go1.8.linux-amd64.tar.gz
17 [root@yinzhengjie local]# cd go/bin/ #进入这个已经被解压的目录中的安装程序中去
18 [root@yinzhengjie bin]# ls -l
19 total 28108
20 -rwxr-xr-x 1 root root 10068917 Feb 17 03:28 go
21 -rwxr-xr-x 1 root root 15226523 Feb 17 03:29 godoc
22 -rwxr-xr-x 1 root root  3481554 Feb 17 03:28 gofmt
23 [root@yinzhengjie bin]# ./go  #执行这个二进制的程序,如果有以下信息说明你已经安装成功了,现在需要做的就是配置环境变量
24 Go is a tool for managing Go source code.
25 
26 Usage:
27 
28         go command [arguments]
29 
30 The commands are:
31 
32         build       compile packages and dependencies
33         clean       remove object files
34         doc         show documentation for package or symbol
35         env         print Go environment information
36         bug         start a bug report
37         fix         run go tool fix on packages
38         fmt         run gofmt on package sources
39         generate    generate Go files by processing source
40         get         download and install packages and dependencies
41         install     compile and install packages and dependencies
42         list        list packages
43         run         compile and run Go program
44         test        test packages
45         tool        run specified go tool
46         version     print Go version
47         vet         run go tool vet on packages
48 
49 Use "go help [command]" for more information about a command.
50 
51 Additional help topics:
52 
53         c           calling between Go and C
54         buildmode   description of build modes
55         filetype    file types
56         gopath      GOPATH environment variable
57         environment environment variables
58         importpath  import path syntax
59         packages    description of package lists
60         testflag    description of testing flags
61         testfunc    description of testing functions
62 
63 Use "go help [topic]" for more information about that topic.
64 
65 [root@yinzhengjie bin]#
66 
67 
68 '''

 3.Go 语言环境变量设置

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 
 9 
10 [root@yinzhengjie ~]# mkdir -pv /yinzhengjie/golang/path/
11 [root@yinzhengjie ~]# pwd  #查看当前路径
12 /root
13 [root@yinzhengjie ~]# more .bash_profile  #编辑当前用户的系统变量
14 # .bash_profile
15 
16 # Get the aliases and functions
17 if [ -f ~/.bashrc ]; then
18 . ~/.bashrc
19 fi
20 
21 # User specific environment and startup programs
22 
23 PATH=$PATH:$HOME/bin
24 
25 export PATH
26 
27 
28 
29 export GOROOT=/yinzhengjie/golang/local/go
30 export GOPATH=/yinzhengjie/golang/path/
31 export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
32 [root@yinzhengjie ~]#
33 [root@yinzhengjie ~]# source .bash_profile  #需要手动的刷新一遍
34 [root@yinzhengjie ~]#
35 [root@yinzhengjie ~]# go env  #如果 出现了以下代码说明你的环境配置已经OK啦
36 GOARCH="amd64"
37 GOBIN=""
38 GOEXE=""
39 GOHOSTARCH="amd64"
40 GOHOSTOS="linux"
41 GOOS="linux"
42 GOPATH="/yinzhengjie/golang/path/"
43 GORACE=""
44 GOROOT="/yinzhengjie/golang/local/go"
45 GOTOOLDIR="/yinzhengjie/golang/local/go/pkg/tool/linux_amd64"
46 GCCGO="gccgo"
47 CC="gcc"
48 GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
49 CXX="g++"
50 CGO_ENABLED="1"
51 PKG_CONFIG="pkg-config"
52 CGO_CFLAGS="-g -O2"
53 CGO_CPPFLAGS=""
54 CGO_CXXFLAGS="-g -O2"
55 CGO_FFLAGS="-g -O2"
56 CGO_LDFLAGS="-g -O2"
57 [root@yinzhengjie ~]#
58 
59 '''

 

 4.关于MAC安装环境我就不必太肥口舌了吧,大家只需要下载想用的软件包,然后解压安装即可,配置和linux配置方法一致。安装包参考下图。

 

三.GO语言的小技巧

1.编写Golang语言的“hello world”

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie go_file]# more hello_world.go   #注意大小写,不然你会进入坑的哟
 9 package main
10 import "fmt"
11 
12 func main(){
13         fmt.Println("hello Yinzhengjie! Welcome to use golang!")  
14 }
15 [root@yinzhengjie go_file]# 
16 [root@yinzhengjie go_file]# go run hello_world.go 
17 hello Yinzhengjie! Welcome to use golang!
18 [root@yinzhengjie go_file]# 
19 '''

 

2.将go程序编译成不同操作系统的中可执行的二进制程序。

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie go_file]# ll
 9 total 4
10 -rw-r--r-- 1 root root 100 Jun  5 16:25 hello_world.go
11 [root@yinzhengjie go_file]#
12 [root@yinzhengjie go_file]# GOOS=linux go build -o hello.linux hello_world.go   #编译成linux系统可以执行的二级制程序
13 You have new mail in /var/spool/mail/root
14 [root@yinzhengjie go_file]#
15 [root@yinzhengjie go_file]# ls -l
16 total 1520
17 -rwxr-xr-x 1 root root 1551621 Jun  5 16:30 hello.linux
18 -rw-r--r-- 1 root root     100 Jun  5 16:25 hello_world.go
19 [root@yinzhengjie go_file]#
20 [root@yinzhengjie go_file]# GOOS=windows go build -o hello.exe hello_world.go   #编译成windows系统可以执行的二进制程序
21 [root@yinzhengjie go_file]#
22 [root@yinzhengjie go_file]#
23 [root@yinzhengjie go_file]# GOOS=darwin go build -o hello.mac hello_world.go   #编译成mac系统可以执行的二进制程序
24 [root@yinzhengjie go_file]#
25 [root@yinzhengjie go_file]# ll  #查看编译成功的程序,即使没有go环境照样是好使的
26 total 4704
27 -rwxr-xr-x 1 root root 1629696 Jun  5 16:30 hello.exe
28 -rwxr-xr-x 1 root root 1551621 Jun  5 16:30 hello.linux
29 -rwxr-xr-x 1 root root 1628192 Jun  5 16:31 hello.mac
30 -rw-r--r-- 1 root root     100 Jun  5 16:25 hello_world.go
31 [root@yinzhengjie go_file]#
32 [root@yinzhengjie go_file]# sz hello.exe  #将编译后的windows程序下载下来到一个没有go环境的windwos操作系统中执行。
33 rz
34 开始 zmodem 传输。  按 Ctrl+C 取消。
35   100%    1591 KB 1591 KB/s 00:00:01       0 Errors
36 
37 [root@yinzhengjie go_file]# 
38 '''

   以下是我用Windows7虚拟机的测试结果:

 

3.还在像python,shell,哪有关心你的代码的格式吗?NO!NO!NO!完全没有这个必要,go语言有一个专门矫正格式的工具,他能够让你的代码看起来特别整齐。

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie go_file]# more hello_world.go  #先看看“张牙舞爪的代码”
 9 package main
10 import "fmt"
11 
12 func main(){fmt.Println("hello Yinzhengjie! Welcome to use golang!")
13 fmt.Println("You are a good boy!")
14 }
15 [root@yinzhengjie go_file]#
16 [root@yinzhengjie go_file]#
17 [root@yinzhengjie go_file]# gofmt hello_world.go  #在看看格式化后的输出,是不是一下就改变了三观,哈哈!
18 package main
19 
20 import "fmt"
21 
22 func main() {
23         fmt.Println("hello Yinzhengjie! Welcome to use golang!")
24         fmt.Println("You are a good boy!")
25 }
26 [root@yinzhengjie go_file]#
27 [root@yinzhengjie go_file]#
28 [root@yinzhengjie go_file]# go run hello_world.go  #代码运行结果如下
29 hello Yinzhengjie! Welcome to use golang!
30 You are a good boy!
31 [root@yinzhengjie go_file]# 
32 '''

 

四.GOlang语言的IDE。

  其实有没有IDE对一个资深的开发工程师来说影响不大(他们打字的速度,真是的快的可怕!这是我身边的牛人,我亲眼所见的!),但是对于一个小白来说,IDE简直就是观世音菩萨,哈哈!因为GO语言是区分大小写的,只要你写错一个关键字的字母的大小写,编译就会出错!但是又IDE的话就可以帮你轻松搞定这样的烦恼。

  我在这里推荐五款给大家(其实我在用的就1和5,喜欢的小伙伴可以选择自己喜欢的风格。):

                        1.atom配合go-plus插件(我的MAC现在用的就是这个);

                        2.sublime配合gosublime插件;

                        3.LitelDe专用IDE,我没有用过啊,别问我,自个百度去;

                        4.emacs + spacemacs配置(相对来说比较麻烦,也是mac的一个不错的选择.)

                        5.Goland JetBrains出品。(这个我是强烈推荐大家使用的,因为我windows用的就是它,哈哈!)

   在网上好多网友都吐槽在windows操作系统下进行Golang编程是很蛋疼的一件事情, 大牛们都比较喜欢用ubantu或是centos又或者是mac编程的。但是我这个人对windows的那是情有独钟啊,说实话我很少在MAC进行编程,工作中用的也都是windows10的操作系统,在windows操作系统下进行编程我觉得挺好的,对于在哪个操作系统下进行编程我不发表个人意见,我觉得“萝卜青菜,各有所爱”,找到适合你自己的那个IDE就好,没有必要纠结别人用哪种IDE工具,只要你自己用着顺手就好。

  如果你是小白的话,我推荐一下我在windows操作系统的下安装的IDE供大家参考:

1.下载地址:https://www.jetbrains.com/go/download/#section=windows

 

2.勾选相应的安装选项

3.自定义Goland语言的工作目录

4.等待安装完毕后,运行Goland

 5.这里我们点击第二个,不导入设置,因为我们是第一次安装.

6.这个是goland程序的一些条款,点击同意即可。

7.创建一个新的目录

8.选择好相应的GO环境并且设置好本地的工作路径

9.创建一个新的文件

10.编写代码,如果可以自动补全说明就没有问题

 

11.用IDE运行你自己写的代码

 

 

五.小试牛刀

  看到这里,估计你把GO的环境已经搞定了,那么好的,跟我一起学习一下几个简单的脚本,让我们来体验一下GO语言的有趣之处!  

 写一个简单的web界面

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 package main
 8 
 9 import (
10     "fmt"
11     "net/http"
12 )
13 
14 func handler(w http.ResponseWriter, r *http.Request)  {
15     fmt.Fprint(w,"Welcome to YinZhengjie's home page!")
16 }
17 
18 func main()  {
19     http.HandleFunc("/",handler)
20     http.ListenAndServe(":8080",nil)
21 }

 

在你的服务器上运行这段代码,然后访问你的主机的"8080"端口,就会有以下的信息输出:

   你有听说过"C10K"吗?如果将外界访问你的某个端口,对其进行相应的操作呢?我们有以下一个案例,挺好玩的。

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 package main
 8 
 9 import (
10     "fmt"
11     "net"
12     "time"
13     "log"
14 )
15 
16 func handle(conn net.Conn){
17     fmt.Fprint(conn,"Welcome to Yin Zhengjie's home page",time.Now().String())
18     conn.Close()
19 }
20 
21 func main()  {
22     l,err := net.Listen("tcp",":6666")
23     if err != nil {
24         log.Fatal(err)
25     }
26     for {
27         conn,err := l.Accept()
28         if err != nil    {
29             log.Fatal(err)
30         }
31         go handle(conn)
32     }
33 }

  你需要在你的服务器上运行以上代码,现在你只需要找一台能够和它互通的服务器来telnet来测试这个端口,看这个端口的反应。我就随便找了2台局域网的来测试。你可以用http的ab工具来进行一下压力测试。

  好了,估计搭建看了以上代码都特别有意思吧,就这么几行轻松实现并发,不想python那么麻烦。而且python实现的并发并不是真正意义上的并发。是不是觉得Golang很牛逼?没错,的确很牛逼。等以后有机会了会继续给大家共享的学习成果的。

 

扩充小知识:

  大家玩过python的小哥哥小姐姐都知道Python的变量一切皆为引用。那么在go语言你可能听说过指针,那么指针和引用有什么区别呢?其实理解起来很简单,我们不从微观的角度去说,因为会把小白越绕越糊涂,我简单句两个例子大家应该就能了解什么是指针了。其实我们在生产环境中经常遇到类似的案例,比如,linux服务器上的软连接。迅雷的种子地址(不要想歪哟,我单纯!)。百度网盘的下载地址等等。其实理解指针你可以把它理解成一种访问路径。

  别人给你发了一部小电影在线传输,这个电影如果有1G,那么如果你网速好的话可能几分钟就搞定了,如果你的网速不好的话,估计30分钟都搞不定。但是如果此时他给你发送的是一个下载地址,可能就需要1s,1后他可以断开和你的链接。你直接根据这个下载地址去找相应的资源即可。下面我们通过一个简单的脚本来学习一下。

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 package main
 8 
 9 import "fmt"
10 
11 func main()  {
12     var x int  //定义一个数字类型的变量
13     var y int
14     x = 100
15     y = 200
16     swap(&x,&y)  //将x.y的值取出来当做位置参数传给swap函数,分别传给了p和q
17     fmt.Print("X==>>>",x,"\n","Y==>>>",y)
18 }
19 
20 func swap(p *int,q *int)  {  //该函数定义要求传入2个参数,p和q
21     var t int  //定义一个数字类型的变量
22     t = *p  //将p的值传给t,此时p的值为100,故此时t和p的值均为100
23     *p = *q  //将q的值传给p,此时q的值为200,故此时p的值有100变为了200
24     *q = t  //将t的值传给q,此刻q的值和t的值均为100,
25 }

 

 

 

 

 

 

  

 

posted @ 2017-03-01 09:29  尹正杰  阅读(8401)  评论(2编辑  收藏  举报