Go Language Environment Installation detailed introduction

Source: Internet
Author: User
Tags install go git clone

Tool Chain Introduction

Go has two sets of compiled Toolchain, which are the GC migrated from PLANT9 and the gccgo dependent on GCC.

The official tool chain provides a binary installation package and source code, you can choose a way to install. The GC tool chain supports the operating system and CPU types as follows:

Operating System CPU Type Notes
FreeBSD 8 or later AMD64, 386, arm Debian Gnu/kfreebsd not supported; Freebsd/arm needs FreeBSD or later
Linux 2.6.23 or later with glibc AMD64, 386, arm Centos/rhel 5.x not supported; No binary distribution for ARM yet
Mac OS X 10.6 or later AMD64, 386 Use the gcc**, comes with xcode**
Windows XP or later AMD64, 386 Use MinGW gcc. No need for Cygwin or msys.

For other operating systems or CPU types, you need to compile the GC toolchain from the source or use GCCGO.

    • If you use CGO, you need to install GCC;
    • The Xcode command tool is part of Xcode and contains the GCC compiler, which you can download from the Componts->downloads dialog box in Xcode.
Installing the binary installation
    1. Download binary packages such as *go1.6.linux-amd64.tar.gz* from official website
    2. Extract to /usr/local directory:

      $ tar-c/usr/local-xzf Go$VERSION.  $GOOS-$GOARCH. tar.gz  
    3. Add/usr/local/go/bin to Path:

      PATH=$PATH:/usr/local/go/bin 

The Go default assumption is installed to/usr/loca/go, and if installed to a different location, you need to set the GOROOT environment variable. For example, if the binary package is extracted to the $home directory, the settings are as follows:

Goroot=$HOME/goPATH=$PATH:$GOROOT/bin     

Note: You need to set the Goroot variable only when you install go to a non-/usr/local directory.

Installing the Go compiler binaries from source compilation

The Go tool chain after version 1.4 is written in the go language, and if you want to build it, the system needs to install the Go compiler:

  • If the system already has the >= 1.4 version of the Go tool chain, the GOROOT_BOOTSTRAP variable is set to the directory in which it resides;

    unset goroot gopath  #如果系统已经有go工具链, need to clear ' $GOPATH ' and ' $GOROOT ' variables;goroot_bootstrap=$HOME/ Local/go   
  • Otherwise, you need to download the 1.4 version of the Go tool chain, which is written in C, only depends on GCC and glibc, you can download binary or compile and install the source code, and then GOROOT_BOOTSTRAP set the variable to the directory;

    Cd/tmp$ git clone [email protected]:golang/go.gitCD go$ git checkout-b 1.4.3 go1.4.3cd src#编译go 1.4. 3goroot_bootstrap#GOROOT \_bootstrap The default value is ' $HOME/go1.4 ', which needs to be redefined if installed to another location;     

Using the Goroot_bootstrap variable to specify the Go tool chain location (if it is at $home/go1.4, you do not need to specify), you can execute the script in the source code to bootstrap.bash generate a $GOOS new toolchain that supports, $GOARCH specifies the target operating system and schema:

$ GOOSgoarch=ppc64./bootstrap.bash 

The tool chain generated by the command is located in the ../../go-${GOOS}-${GOARCH}-bootstrap. directory and can be set to GOROOT_BOOTSTRAP the value of the variable for subsequent compiling of the source code;

Compiling the latest Go source code
    1. Set git proxy:

       $ git config http.proxy http://user:[email protected]:p Ort$ git config https.proxy https://user:[email protected]:p ort   
    2. Set Go Get proxy

       $ export http_proxy=http://user : [email protected]:p ort     
    3. Get source code:

      Download directly or clone from Git repository.

      • Download from official website

         $ wget https://golang.org/dl/go$ Version.src.tar.gz$ TAR-XZVF go $VERSION.  $OS- $ARCH. tar.gz       
    • Clone from Git code base:

        #需FQ  $ git clone https://github.com/golang/go.git  cd go  #也可以切换到其它分支如master  
  1. Compiling source code

    pwd/tmp/CD go/src$./all.bash for  linux/amd64 in/tmp/goinstalled commands in/tmp/go/bin*** You need to Add/tmp/go/bin to your PATH.  

    Go will log the installation location to the binary goroot variable, and if you need to adjust the installation directory, you can set $GOROOT _final=/path/to/gotree so that when you are finished compiling you will be prompted to /tmp/go is moved to the
    /path/to/gotree directory (this parameter is valid only at compile time, if you move go tree after compilation refer to step 5).

    $ ls/tmp/go-fapi/  AUTHORS  bin/  CONTRIBUTORS  doc/  favicon.ico  include/  lib/license  misc/  Patents  pkg/  README  robots.txt  src/  test/  VERSION$ ls/tmp/go/bin  #源码包自带的二进制工具命令, no godocgo  gofmt$ ls/tmp/go/pkg/tool/linux_amd64/addr2line  asm  CGO  Compile  dist  doc  fix  link  nm  objdump  pack  pprof Tour  Yacc' Goroot| Gotooldir 'goroot=#可见Go tree is installed to the expected position gotooldir="/tmp/go/pkg/tool/linux_amd64" 

    will be /tmp/go/bin added to path and ready to use.

  2. Move the Go source directory

    To move the compiled go tree to a different directory, and then modify the goroot environment variable.

     $ Mkdir/tmp/xxx$ export goroot=/tmp/xxx$ MV */tmp/xxx$/tmp/xxx/bin/go env |grep-e Span class= "S1" > ' goroot| Gotooldir ' goroot= "/tmp/xxx"  #Go tree and tool chain automatic adjustment Span class= "NV" >gotooldir= "/tmp/xxx/pkg/tool/linux_amd64" # Set PATH and Gopath$ export path=/tmp/xxx/bin: $PATH $ which Go/tmp/xxx/bin/go$ go Versiongo version go1.4 linux/amd64
                                                
  3. Install additional tools such as godoc , vet cover (these tools are included in the binary release, no additional installation is required):

    Some go tools are located in the Go.tools warehouse and require additional installation.

    #安装所有工具:# ... is a wildcard character, refer to: Go Help packages#只安装godoc工具#多了godocgo godoc gofmt#多了vet, coveraddr2line ASM CGO compile cover Dist D OC Fix link NM objdump pack pprof Tour trace vet YACC   

    The GO command will godoc install to $GOROOT/bin or $GOBIN , for go tool example, cover install to vet $GOROOT/pkg/tool/$GOOS_$GOARCH . You can use go tool cover or go tool vet command to invoke a program in a later directory.

Tool chain Testing
  1. Create and set Gopath (not required):

    $HOME/go/{src,bin,pkg}gopath=$HOME/go$HOME/go/src/demoCD!  $
  2. Write a test file such as Hello.go

    "FMT" Func main{  fmt. Printf("Hello, world\n")}    
  3. Compile and execute, the-X option can print out the compilation process

    $ go build-x demo.goWork=/tmp/go-build333633893mkdir-p  $WORK/command-line-arguments/_obj/mkdir-p  $WORK/command-line-arguments/_obj/exe/cd/home/ksyun/golang/src/home/ksyun/local/go /pkg/tool/linux_amd64/compile-o  $WORK/command-line-arguments.a-trimpath  $WORK- P main-complete-buildid ed5feda32ea5b5ab51ac7fe9d1193005f6f99836-d _/home/ksyun/golang/src-i $ Work-pack./demo.goCD./home/ksyun/local/go/pkg/tool/linux_amd64/link-o  $WORK/ Command-line-arguments/_obj/exe/a.out-l  $WORK-extld=gcc-buildmode =exe-buildid=ed5feda32ea5b5ab51ac7fe9d1193005f6f99836  $WORK/ COMMAND-LINE-ARGUMENTS.AMV  $WORK/command-line-arguments/_obj/exe/a.out Demohello, World  
environment variable (optional)

The compiler tool chain can be configured with the following environment variables;

  • $GOROOT
    At build time, the value is the all.bash parent directory of the directory where the script is located, which is written to the generated binary, and if the installation directory is moved later, the new go Tree top-level directory is specified with that variable;

  • $GOROOT_FINAL
    General and $GOROOT consistent, define the location to be installed in the post-build installation phase;

  • $GOOSAnd$GOARCH
    When cross-compiling, the target operating system and architecture are defined separately, default and $GOHOSTOS $GOHOSTARCH consistent. The combinations are as follows:

    $GOOS$GoarchAndroidArmDarwin386DarwinAmd64DarwinArmDarwinArm64DragonflyAmd64Freebsd386FreebsdAmd64FreebsdArmLinux386LinuxAmd64linux armlinux arm64linux ppc64linux ppc64lelinux mips64 linux mips64lenetbsd 386netbsd span class= "n" >amd64netbsd armopenbsd 386openbsd amd64openbsd armplan9 386plan9 amd64solaris amd64< span class= "n" >windows 386windows amd64     
    #交叉编译$ GOOSgoarch=amd64 Go build(console) mono/.net Assembly    
  • $GOHOSTOSAnd$GOHOSTARCH
    The operating system and schema type of the host on which the tool chain is compiled must be compatible with the operating system and CPU architecture type;

  • $GOBIN
    If set, all the go binaries will be installed to this directory instead of the default$GOPATH/bin

Reference
      1. Getting Started
      2. Installing Go from Source

Go Language Environment Installation detailed introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.