[Go] 環境變量,模塊化與基礎語法

 

【環境變量】html

安裝完 go 以後,設置必要環境變量:git

export GOPATH=/home/wc/go-lab
export GO111MODULE=on
export GOPROXY=https://goproxy.io
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin:/usr/local/protobuf/bin

 

【模塊化】github

`export GO111MODULE=on` 開啓 go 模塊化包管理,將再也不使用 GOPATH 管理依賴。app

`go mod init /home/wc/go-lab/go-syntax` 用來初始化模塊目錄。模塊化

go.mod 不能在 GOPATH 目錄下,go.sum 是當前模塊依賴項目的版本鎖定。函數

 

【基礎語法】ui

// 定義包名
package main

// 須要使用的包
import "fmt"

// go run first.go 編譯並執行,不會留下可執行文件
// 或者
// go build -o binary/first first.go && ./binary/first

// 程序開始執行的函數, main 函數是每個可執行文件必須包含的
func main () {
    // 當標識符(常量/變量/類型/函數名/結構字段)以一個大寫字母開頭,那麼這種形式的對象就能夠被外部包的代碼導入使用
    // 標識符以小寫字母開頭,則對包外不可見,但在整個包的內部是可見而且可用的.
    // GO的字符串鏈接 +
    fmt.Println("Google " + "lang")

    var apple string
    var orange string
    var fruit string

    apple = "Apple"
    orange = "Orange"
    fruit = apple + orange;

    fmt.Println("Fruit " + fruit);
}

/*
行分割符
註釋
標識符:命名變量,類型等程序實體
字符串鏈接
關鍵字
*/

// 關鍵字
/*
break, default, func, interface, select,
case, defer, go, map, struct,
chan, else, goto, package, switch
const, fallthrough, if, range, type,
continue, for, import, return, var,
*/

// 預約義標識符
/*
append, bool, byte, cap, close, complex, complex64, complex128, uint16
copy, false, float32, float64, imag, int, int8, int16, uint32
int32, int64, iota, len, make, new, nil, panic, uint64
print, println, real, recover, string, true, uint, uint8, uintptr
*/

 

Src:https://github.com/farwish/go-lab/blob/master/go-syntax/first.gospa

Link:http://www.javashuo.com/article/p-pvxlumrc-dq.htmlcode

相關文章
相關標籤/搜索