讀《Introducing Go》O'Reilly 第 6 ~ 11 章

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。一,開始二,類型三,變數四,控制結構五,數組,切片(slice)和字典(maps)六,函數七,結構和介面八,包九,測試十,並發十一,下一步

第 6 章,函數 43頁函數通常表示為一個黑盒子
func average(xs []float64) float64 {   panic("not implemented")}
函數參數是一個數組,返回一個浮點64 數。func average(xs []float64) float64 {   total := 0.0   for _, v := range xs {      total += v   }   return total / float64( len(xs) )}
func main() {   xs := []float64{98, 93, 77, 82, 83}   fmt.Println( average(xs) )}可變個數參數函數。func add(args ...int) int {   total := 0   for _, v := range args {          total += v   }   return total}
結束 closure遞迴 recursionfunc factorial(x uint) uint {
  if x == 0 {
    return 1
  }
  return x * factorial(x-1)
}

defer, panic and recoverfunc main() {
  defer second() //順延強制
  first()
}

* 和 & 操作&引用變數地址,*指向變數地址(指標)new 對象執行個體化
第7章,結構和介面 55頁結構type Circle struct {  x float64  y float64  r float64}或type Circle struct {   x, y, r float64}初始化var c Circle
c := new(Circle)c := Circle{x: 0, y: 0, r: 5}c := Circle{0, 0, 5}c := &Circle{0, 0, 5}
結構元素的引用c.x , c.y
方法:func (c *Circle) area() float64 {   return math.Pi * c.r * c.r}
介面 59頁http://www.cnblogs.com/hustcat/p/4007126.html

包 63頁import "fmt"import (    "fmt"    "math")
"strings"strings.Contains("test", "es")// true
strings.Count("test", "t")// 2
輸入、輸出  67頁檔案和檔案夾package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("test.txt")
if err != nil {
// handle the error here
return
}
defer file.Close()
// get the file size
stat, err := file.Stat()
if err != nil {
return
}
// read the file
bs := make([]byte, stat.Size())
_, err = file.Read(bs)
if err != nil {
return
}
str := string(bs)
fmt.Println(str)
}
錯誤 70頁package main
import "errors"
func main() {
  err := errors.New("error message")
}
排序鏈表 71頁雜湊和加密 73頁
服務 servers, tcp 75頁http 77頁rpc 78頁文檔化 81 頁
第9章 測試 83頁第10章,並發 87頁channels 89頁第11章,下一步golang.org/src/pkg/io/ioutil/ioutil.go
網站打不開,被牆了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.