Ubuntu下搭建Go語言開發環境

jopen 10年前發布 | 159K 次閱讀 Go語言 Google Go/Golang開發

    學習一下Go語言。學習之前,首先要搭建一下Go語言的開發環境,這篇文章主要介紹一下搭建環境的過程。

一、安裝Go語言依賴包

Go語言部分功能是用C語言開發的,所以安裝Go之前需要安裝gcc,make等依賴,ubuntu下的安裝命令如下:

sudo apt-get install bison ed gawk gcc libc6-dev make

二、獲取Go源碼

成功安裝了Go語言的依賴之后,就需要下載Go語言的源碼包,方法比較多:可以通過hg拷貝源碼,也可以到官網上直接下載對應的源碼包

1、使用hg拷貝

這種方式需要先安裝Mercurial,Mercurial是類似git的版本管理系統,簡稱hg(水銀),安裝命令如下:

sudo apt-get install python-setuptools
sudo apt-get install python-dev
sudo apt-get install build-essential
sudo apt-get install mercurial

安裝完成之后,通過下面的命令拷貝代碼:

cd ~/
hg clone -r release https://go.googlecode.com/hg/ go

2、直接下載

Go語言官方下載地址:官方

由于被墻的原因,官方下載地址很難正常打開,所以我將自己下載的Ubuntu 32位的源碼包放在了百度網盤上,安裝環境和我一樣的同學可以直接下載這個:百度網盤

下載之后將壓縮包解壓到HOME目錄下。

三、配置Go環境變量

在編譯安裝之前,還需要設置Go語言的環境變量,在Ubuntu下的配置方法如下:

1、打開HOME目錄下的.bashrc文件

vim ~/.bashrc

2、在文件的最后添加以下內容

export GOROOT=$HOME/go
export GOBIN=$GOROOT/bin
export GOARCH=386
export GOOS=linux
export PATH=$GOROOT/bin:$PATH
export GOPATH=$HOME/workspace/Go

3、保存設置并運行命令使其生效

source ~/.bashrc

四、編譯Go源碼包

以上步驟完成之后就可以執行編譯腳本了,編譯Go語言的命令如下:

cd $GOROOT/src
./all.bash

編譯會執行大概10分鐘的時間,當你看到以下信息的時候,就表示安裝成功了:

ALL TESTS PASSED


Installed Go for linux/386 in /home/wuxianglong/go. Installed commands in /home/wuxianglong/go/bin.</pre>

五、Hello World!

在學習一門新的語言時,我們寫的第一行代碼一般是Hello World,這里也不例外。在執行完上述的步驟之后,你可以在命令行輸入 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     env         print Go environment information     fix         run go tool fix on packages     fmt         run gofmt on package sources     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     filetype    file types     gopath      GOPATH environment variable     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.</pre>

之后,可以寫一個Hello World程序,代碼如下:

package main

import "fmt"

func main() {     fmt.Printf("Hello World!\n") }</pre>

將上面的代碼保存在文件hello.go中,然后在命令行執行:

go run hello.go

 輸出結果:Hello World!</pre>

OK!Go語言的開發環境已經安裝成功了!

六、其他安裝方式

以上介紹的Go語言安裝方法是通過編譯源碼安裝的,其實在Ubuntu下也可以通過apt-get方式安裝,安裝命令如下:

sudo apt-get install golang

如果上面的命令安裝出錯的話,可以嘗試一下下面的命令:

sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang-stable

 

參考文章

Go介紹和安裝

Ubuntu 配置 Go 語言開發環境(Sublime Text+GoSublime)

 

Over!

</div> 本文地址:http://xianglong.me/article/ubuntu-install-golang-env/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!