讀《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
網站打不開,被牆了。