從原始碼安裝Go1.6到CentOS 7

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

在中國網路環境下從原始碼安裝Go1.6到CentOS 7

https://github.com/northbright/Notes/blob/master/Golang/china/install-go1.6-from-source-on-centos7-in-china.md

背景

  • 在當前的中國網路環境下,我們無法訪問Google的服務的,包括Golang.org。
  • 從第三方網站下載先行編譯的二進位Go發行版可能存在第三方原始碼注入的風險,例如之前的XcodeGhost。
  • Go的權威倉庫地址是https://go.googlesource.com/go,同時還有一個鏡像倉庫在https://github.com/golang/go。
  • 至少在目前,我們還能訪問github:-)

問題

  • Go1.6的編譯過程需要Go1.4的二進來實現 bootstrap(自舉)(簡單來說: Go需要Go自身來編譯)。

解決方案

  • 從Github的Go倉庫鏡像擷取Go的原始碼。
  • 首先編譯Go1.4(只需要gccglibc-devel,不需要Go來編譯)。
  • 使用編譯好的Go1.4的二進位檔案來編譯Go1.6。

步驟

  1. 如果之前已經安裝過老版本的Go,清除$GOPATH, $GOROOT變數。

  2. 安裝好Git。

    • 配置 Git
      • git config --global user.email "email-for-github"
      • git config --global user.name "user name"
    • 建立SSH key,添加Public Key到Github帳號。
      • ssh-keygen -t rsa -b 2048
      • 複製~/.ssh/id_rsa.pub中的Public Key到github SSH settings以添加新的SSH key
  3. 安裝 gccglibc-devel

    • sudo yum install gcc glibc-devel
  4. 從原始碼編譯安裝Go1.4

    • cd ~/
    • git clone git@github.com:golang/go.git
    • cd go
    • git checkout -b 1.4.3 go1.4.3
    • cd src
    • ./all.bash
  5. 複製 ~/go 到 $GOROOT_BOOTSTRAP(預設值是~/go1.4

    • cp ~/go ~/go1.4 -rf
  6. 從原始碼編譯安裝Go1.6

    • cd ~/go
    • git clean -dfx
    • git checkout -b 1.6 go1.6
    • cd src
    • ./all.bash
  7. 設定 $GOPATH 以及添加Go二進位路徑到 $PATH

    • sudo vi /etc/profile

        # Golang Env  export PATH=$PATH:/home/xx/go/bin  export GOPATH=/home/xx/go-projects
  8. 重啟和測試

    • sudo reboot
    • go version

        go version go1.6 linux/amd64

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.