Introducing Go - O'Reilly 2016 閱讀筆記

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Introducing Go

目錄

  • 1Get Started
  • 2類型
  • 3變數
  • 4控制結構
  • 5Arrays, Slices, and Maps
  • 6Functions
  • 7Structs and Interfaces
  • 8包
  • 9Testing
  • 10並發
  • 11Next Steps

Get Started

類型

變數

控制結構

  1. for i<=10 { ... } //for關鍵字這裡用作while
  2. if:這裡為什麼不加()呢?跟Swift學的?還是跟Python?

Arrays, Slices, and Maps

  1. x := make(map[string]int) //map的預設值是nil,所有必須先make,才能往裡面add元素
  2. 嵌套的map(JSON?):elements := map[string]map[string]string { ...

Functions

  1. 變參://這到底是運行時還是編譯器的?
    1. func Println(a ...interface{}) (n int, err error)
    2. xs := []int{1,2,3}; fmt.Println(add(xs...))
  2. 閉包
    1. 支援閉包的前提是:外部參考變數是heap上分配的,但Go可以直接返回函數和C語言只能返回函數指標有什麼不同?
  3. defer, panic, recover
  4. *與&

Structs and Interfaces

  1. c := &Circle{0, 0, 5} //這可能是Go語言裡struct的最正常用法?
  2. 因為參數都是傳值的(?),所以函數一般使用指標來傳參?//C語言不也是傳值的嘛,C++裡可以傳引用&
  3. func (r *Rectangle) area() float64 { ... }
    1. ==> r := Rectangle{0, 0, 10, 10}; r.area()
  4. 表達is-a:嵌入類型(好像另外那個語言也是這種匿名風格的?)
  5. Interfaces
    1. type Shape interface { area() float64 }

  1. strings
    1. strings.Contains("test", "es") //為什麼不是直接在string對象上擴充方法?
    2. strings.Join([]string{"a","b"}, "-")
    3. strings.Replace("aaaa", "a", "b", 2) //最後一個參數代表最多替換個數?為什麼不是startIndex呢
  2. IO:Reader與Writer
    1. func Copy(dst Writer, src Reader) (written int64, err error)
    2. bytes.Buffer
  3. Files and Folders
    1. file, err := os.Open("test.txt") //rw?
    2. stat, err := file.Stat() ==> stat.Size()
    3. os.Open(".").Readdir(-1) //這個代碼有點太低級了
    4. 使用包path/filepath的目錄遍曆:
      1. filepath.Walk(".", func(path string, info os.FileInfo, err error) error { ...(可返回filepath.SkipDir停止遍曆) } //似乎沒有控制選項啊?
  4. container/list://雙鏈表??有沒有進階一點的資料結構
  5. sort.Sort
    1. 需要(數組)對象實現3個方法:Len() int、Less(i, j int) bool、Swap(i, j int) //Go語言支援a,b=b,a的交換賦值文法
  6. hash與密碼學*
  7. Servers
    1. TCP
      1. listener, err := net.Listen("tcp", ":9999")
      2. for { con, err := listener.Accept() //嗯?必須收到一個串連才能收到下一個嗎?
      3. err := gob.NewDecoder(c).Decode(&msg) //var msg string; gob是什麼編解碼演算法?
      4. 用戶端:
        1. c, err := net.Dial("tcp", "127.0.0.1:9999")
    2. HTTP
      1. http.HandleFunc("/hello", hello) //func hello(res http.ResponseWriter, req *http.Request) { ... }
      2. http.ListenAndServe(":9000", nil)
      3. 靜態檔案:http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")),),) //這裡是不是多了幾個逗號?
    3. RPC(略)
  8. 解析命令列參數
    1. flag.Parse()

Testing

  1. _test.go
  2. import "testing"
    1. func TestAverage(t *testing.T) { ...t.Error(...)... }
  3. $ go test

並發

  1. <建立channel,並傳遞給goroutine>
  2. select {
    1. case <- time.After(time.Second): //逾時
  3. c := make(chan int, 1) //緩衝的channel是非同步(但是超出限制仍然會阻塞吧)
  4. 發起http GET請求:res, err := http.Get(url) //這個也太簡潔了吧?(沒有請求Header的設定)

Next Steps

  1. 閱讀Go pkg下的代碼實現?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.