您的位置:首页 > 编程语言 > Go语言

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

2017-06-07 19:19 330 查看
Go语言是一种开源的编程语言,它旨在使我们能够方便地构建简单、 可靠、 高效的软件。


安装

下载地址:

1.1. https://golang.org/dl/

1.2. https://dl.gocn.io

编译安装(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等。

具体使用啥,熟悉哪个用哪个
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  golang