Hello World

Go簡(jiǎn)介

Go是Google開發(fā)的一種靜態(tài)強(qiáng)類型、編譯型、并發(fā)型,并具有垃圾回收功能的編程語(yǔ)言。因其天生并發(fā)、編譯后的二進(jìn)制文件可以直接移植運(yùn)行,這使得其與Docker相結(jié)合可以創(chuàng)建完美的微服務(wù)。

編程風(fēng)格

Go有幾項(xiàng)強(qiáng)制性的編程規(guī)定,可以幫助程序員強(qiáng)制養(yǎng)成書寫可閱讀的代碼和減少程序中的bug(C語(yǔ)言中忘記(;)是最廣泛的bug)

  1. 每行程序結(jié)束后不需要撰寫分號(hào)(;)。
  2. 大括號(hào)({)不能夠換行放置。
  3. if判斷式和for循環(huán)不需要以小括號(hào)包覆起來(lái)。

代碼組織

Go的代碼組織結(jié)構(gòu)如下,Go與其他語(yǔ)言代碼組織不同的是Go內(nèi)置使用Git進(jìn)行版本控制,在編寫庫(kù)文件時(shí)可以直接放在自己的Github上,然后供自己和其他人下載使用。

$GOPATH
├─── bin/                                   # 放置 go install 生成的可執(zhí)行文件
|    └─── hello                             
├─── pkg/                                   # 放置 go install 生成的庫(kù)對(duì)象文件
|    └─── linux_amd64/github.com/
|         ├─── gorilla/mux/
|         |    └─── mux.a 
|         └─── lucky-loki/rect/
|              └─── rect.a                       
└─── src/                                    # 放置庫(kù)文件
     └─── github.com
          ├─── gorilla/mux/                   # 外部 Git 庫(kù)
          |    ├─── .git/             
          |    ├─── mux.go
          |    └─── ...
          └─── lucky-loki/                    # 工作區(qū)                 
               ├─── hello/
               |    └─── hello.go             # 主程序
               └─── rect/
                    ├─── rect.go              # 自己編寫的庫(kù)文件
                    └─── rect_test.go         # 自己編寫的單元測(cè)試文件

示例

編寫你的第一個(gè)庫(kù)

mkdir -p $GOPATH/src/github.com/lucky-loki/rect
cd $GOPATH/src/github.com/lucky-loki/rect
vim rect.go
package rect

import "fmt"

type rect struct {
    width int
    height int
}

func (r *rect) area() int {
    return r.width * r.height
}

func Print() bool{
    r := rect{ width:10, height:5}
    fmt.Printf("area: %d\n", r.area())
    return true
}
# 單元測(cè)試*_test.go
vim rect_test.go
package rect

import (
    "fmt"
    "testing"
)

// 測(cè)試函數(shù)模版 func TestXXX(t *testing.T), 測(cè)試失敗使用t.Error或者t.Fail
func TestRect(t *testing.T) {
    fmt.Println("In test rect")
    ret := Print()
    if ret != true {
        t.Errorf("one error happened")
    }
}
  • go build不會(huì)產(chǎn)生輸出文件,它會(huì)將編譯后的包放在本地編譯緩存中。
  • go install會(huì)將編譯后的包放在pkg文件夾中。
# 編譯庫(kù)文件,成功后進(jìn)行單元測(cè)試
cd $GOPATH/src/github.com/lucky-loki/rect
go build && go test
# 或者
go build github.com/lucky-loki/rect
go test github.com/lucky-loki/rect

編寫主程序

mkdir -p $GOPATH/src/github.com/lucky-loki/hello
cd $GOPATH/src/github.com/lucky-loki/hello
vim hello.go
  • 主程序必須使用package main作為包名。
  • import "<package>"會(huì)在$GOROOT/src$GOPATH/src下進(jìn)行包查找。
package main

import (
    "fmt"
    "github.com/lucky-loki/rect"
)

func main() {
    fmt.Print("Hello World!")
    rect.Print()
}
# 編譯主程序,運(yùn)行
cd $GOPATH/src/github.com/lucky-loki/hello
go build && ./hello
# 或者
go build github.com/lucky-loki/hello && ./hello
go install github.com/lucky-loki/hello && $GOPATH/bin/hello

發(fā)布你的庫(kù)

cd $GOPATH/src/github.com/lucky-loki/rect
git init
git add rect.go rect_test.go
git commit -m "initial commit"
git remote add origin https://github.com/lucky-loki/rect.git
git push -u origin master

下載遠(yuǎn)程庫(kù)

go get會(huì)先將遠(yuǎn)程庫(kù)下載存儲(chǔ)到$GOPATH/src下,然后執(zhí)行go install進(jìn)行庫(kù)編譯生成對(duì)象文件放在$GOPATH/pkg下或者執(zhí)行主程序編譯放在$GOPATH/bin下。

go get github.com/gorilla/mux
# 如何導(dǎo)入
# import "github.com/gorilla/mux"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容