Golang入门-----安装配置篇

Go语言是一种开源的编程语言,它旨在使我们能够方便地构建简单、 可靠、 高效的软件。

安装

  1. 下载地址:

    1.1. https://golang.org/dl/
    1.2. https://dl.gocn.io

  2. 编译安装(Linux环境)

# go1.4.3版本依赖于GCC环境

$ cd ~
$ mkdir {local,go}
$ tar -zxvf go1.4.3.src.tar.gz -C /home/go/local/ 
$ cd local
$ mv go/ go1.4.3/
$ cd go1.4.3/src
$ ./make.bash
$ tar -zxvf go1.8.linux-amd64.tar.gz -C /home/go/local/
$ cd ~/local/go/
$ export GOROOT_BOOTSTRAP=/home/go/local/go1.4.3/
$ ./make.bash 

#配置环境变量
$ ~/.bashrc
export GOROOT=$HOME/local/go
export GOPATH=$HOME/go
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

$ source ~/.bashrc

#验证安装是否成功
$go version    #版本
go version go1.8 linux/amd64

$go env        #环境变量
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/go/go"
GORACE=""
GOROOT="/home/go/local/go"
GOTOOLDIR="/home/go/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build156894198=/tmp/go-build"
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"

基本使用

1.基本语法

$ go --help
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.

2.测试案例

$ cd ~/go
$ vi hello.go
package main

import "fmt"

func main() {
        fmt.Println("Hello,golang")
}

$ go run hello.go 
hello world

3.跨平台编译
go可以实现一个平台开发,编译为多个平台版本。同平台随意拷贝执行。

#windows平台,可以放在windows环境执行        
$GOOS=windows go build -o hello.windows.exe helloworld.go 
#Mac平台,可以放在Mac环境执行    
$GOOS=darwin go build -o hello.mac helloworld.go 
#Linux平台
$GOOS=linux go build -o hello.linux helloworld.go 

4.格式化代码

#方法1:gofmt格式化
$gofmt -w hello.go

#方法2:goimports格式化代码,但是 goimports 不是自带的,需要额外安装,而且可以补齐缺少的import对象。
安装goimports:
$ mkdir -p $GOPATH/src/golang.org/x
$ cd $GOPATH/src/golang.org/x
$ git clone https://github.com/golang/tools.git
$ go install golang.org/x/tools/cmd/goimports 

$ goimports -w hello.go

IDE工具选择

1、Atom配合go-plus插件。
2、LitelDE,专用IDE。
3、emacs + spacemacs配置
4、Eclipse等。

具体使用啥,熟悉哪个用哪个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值