go瀛︿範绗旇

go鐨勯樆濉烇紝閫氫俊锛岄攣锛屾暟鎹被鍨嬶紙interface锛夛紝gmp鐨勫師鐞嗭紝slice鐨勫師鐞嗘庝箞鎵╁锛宮ap鐨勫師鐞嗭紝鎬ц兘浼樺寲锛坧prof锛
make鍜宯ew鐨勫尯鍒
golang鐨勫唴瀛樼鐞
鍑芥暟浼犲叆缁撴瀯浣撲紶鍊艰繕鏄紶鎸囬拡 浼犳寚閽堝彲鑳戒細鍐呭瓨閫冮告爤涓婇冮稿埌鍫嗕笂
绾跨▼妯″瀷 goroutine鍘熺悊

涓嶄竴鏍风殑鍦版柟 go

璇硶
– 鍙橀噺
– 閫夋嫨 寰幆
– 鎸囬拡锛屾暟缁勶紝瀹瑰櫒

闈㈠悜鎺ュ彛
– 缁撴瀯浣
– duck typeing
– 缁勫悎鐨勬濇兂
鍑芥暟鏃剁紪绋
– 闂寘

宸ョ▼鍖
璧勬簮绠$悊锛岄敊璇鐞
娴嬭瘯鍜屾枃妗
鎬ц兘璋冧紭

骞跺彂缂栫▼
goroutine 鍜宑hannel
鐞嗚В璋冨害鍣
澶氭牱鐨勪緥棰

2.1 鍙橀噺瀹氫箟
println 鍙兘鍙橀噺
printf 鍙互鍜岀櫨鍒嗗彿

var鍏抽敭瀛

:=瀹氫箟鍙橀噺

a,b,i,s1,s2 := true, false, 3, “hello”, “world”
鍙兘鍦ㄥ嚱鏁板唴浣跨敤

2.2 鍐呭缓鍙橀噺绫诲瀷
– bool,string
– (u)int,(u)int8,(u)int16,(u)int32,(u)int64,uintptr(鏍规嵁鎿嶄綔绯荤粺鏉)
– byte, rune
– float32, float64, complex64, complex128(瀹為儴铏氶儴128)
2.3 甯搁噺涓庢灇涓

const filename = "abc.txt"
const 鏁板煎彲浣滀负鍚勭绫诲瀷浣跨敤
const a, b = 3,4
var c int = int(math.Sqrt(a*a + b*b))

2.4 鏉′欢璇彞

if

if contents, err := ioutil.ReadFile(filename); err==nil{
    fmt.Println(string(content))
} else {
    fmt.Println("cannot print file contents:", err)
}

switch

涓嶉渶瑕乥reak锛屽彲浠ョ洿鎺witch澶氫釜鏉′欢

func eval(a, b int, op string) int {
    var result int 
    switch op {
        case "+":
            result = a + b
        case "-":
            result = a - b
        case "*":
            result = a * b
        case "/":
            result = a / b
        default:
            panic("unsupported operator" + op)
    }
    return result
}

func grade(score int) string  {
    grade := ""
    switch  {
    case score<60:
        grade = "f"
    case score<70:
        grade = "b"
    case score<100:
        grade = "a"
    default:
        panic(fmt.Sprintf("wrong score %d", score))
    }
    return grade
}
panic: wrong score 100

goroutine 1 [running]:
main.grade(0x64, 0x4a1bc0, 0xc00006c058)
    /mnt/d/go/spider/basic.go:29 +0x105
main.main()
    /mnt/d/go/spider/basic.go:36 +0x2a
exit status 2

for

sum :=0
for i:=1; i<=100; i++{
    sum += 1
}

2.6 鍑芥暟

func eval(a, b int, op string) int {
    switch op {
    case "+":
        result = a + b
    case "-":
        result = a - b
    case "*":
        result = a * b
    case "/":
        result = a / b
    default:
        panic("unsupported operator" + op)
    }
}

func div(a, b int) (int, int) {
    return a / b, a % b
}
func div(a, b int) (q, r int) {
    return a / b, a % b 
}
func (a int, b int) int {
    return int(math.Pow(float64(a), float64(b)))
}

鍑芥暟鐨勫弬鏁颁篃鍙互鏄嚱鏁

func apply(op func(int, int) int, a, b int) int {
    fmt.Printf("Calling %s with %d, %d\n", runtime.FuncForPC(reflect.ValueOf(op).Pointer().Name(), a, b))
    return op(a, b)
}

浼犲叆澶氫釜鍙傛暟

func sum(values ...int) int {
    sum := 0 
    for i range values {
        sum += value[i]
    }
    return sum
}

2.7 鎸囬拡

var a int =2
var pa *int = &a
*pa =3
fmt.Println(a)

go 鍙湁鍊间紶閫掍竴绉嶆柟寮
var cache Cache

func swap(a, b int) {
    b,a = a,b
}

3.1 鏁扮粍,鍒囩墖鍜屽鍣

func main() {
    var arr1 = [5]int
    arr2 := [3]int{1,3,5}
    arr3 := [...]int{2,4,6,8}
    var grid [4][5]int
}

鏁扮粍鐨勯亶鍘

sum :=0
for _,v := range numbers{
    sum += v 
}

鎰忎箟鏄庣‘锛岀編瑙
鏁扮粍鏄肩被鍨嬨
– [10]int 鍜孾20] int 鏄笉鍚岀被鍨
– 璋冪敤func f(arr[10]int) 浼氭嫹璐濇暟缁

3.2 slice(鍒囩墖)
arr := […]int{0, 1, 2, 3, 4, 5, 6, 7}
s := arr[2:6]
slice 鏈韩娌℃湁鏁版嵁锛屾槸瀵瑰簳灞俛rray鐨勪竴涓獀iew

slice鐨勬嫇灞
arr := […]int{0,1,2,3,4,5,6,7}
s1 := arr[2:6]
s2 := s1[3:5]

ptr len cap
slice 鍙互鍚戝悗鎷撳睍锛屼笉鍙互鍚戝墠鎷撳睍
s[i] 涓嶅彲瓒呰秺len(s),鍚戝悗鎷撳睍涓嶅彲浠ヨ秴瓒婂簳灞傛暟缁刢ap(s)

娣诲姞鍏冪礌
– 娣诲姞鍏冪礌鏃跺鏋滆秴瓒奵ap,绯荤粺浼氶噸鏂板垎閰嶆洿澶х殑搴曚粨鏁扮粍
– 鐢变簬鍊间紶閫掔殑鍏崇郴锛屽繀椤绘帴鍙梐ppend鐨勮繑鍥炲

s3 = append(s2, s3[:2]...)

澶嶅埗鍏冪礌

copy(c2, c3)

3.4 map

m := map[string]string{
    "name": "ccmouse"
}
m1 := make(make[string]int) //m1== empty map
var m2 = map[string]int //m2 ==nil

for _, v := range m{
    fmt.Println("key,value,", v)
}
mm, ok:= m["m"]

瀵绘壘鏈闀夸笉鍚湁閲嶅瀛楃鐨勫瓙涓

3.6 瀛楃鍜屽瓧绗︿覆澶勭悊

rune 鐩稿綋浜 go鐨刢har
– 浣跨敤rango閬嶅巻pos锛宺une瀵
– utf8.RuneCountInString 鑾峰緱瀛楃鏁伴噺
– len鏄瓧鑺傞暱搴
– []byte鑾峰緱瀛楄妭
– Fields ,Split,Join
– Contains, Index
– ToLower, ToUpper
– Trim, TrimRight, TrimLeft

4.1 缁撴瀯浣撳拰鏂规硶
闈㈠悜瀵硅薄
– go璇█浠呮敮鎸佸皝瑁咃紝涓嶆敮鎸佺户鎵垮拰澶氭
– go璇█娌℃湁class 鍙湁struct

type point struct {
    i, j int
}

缁撴瀯鐨勫垱寤

func createNode(value int) *treeNode {
    return &treeNode{Value:value}
}
root.left.right = createNode(2)

涓虹粨鏋勫畾涔夋柟娉

func(node TreeNode) print() {
    fmt.Print(node.Value)
}

浣跨敤鎸囬拡浣滀负鏂规硶鎺ユ敹鑰

func(node *TreeNode) setValue(value int) {
    node.Value = value
}

鍊兼帴鏀惰 vs 鎸囬拡鎺ユ敹鑰
– 瑕佹敼鍙樺唴瀹瑰繀椤讳娇鐢ㄦ寚閽堟帴鏀惰
– 缁撴瀯杩囧ぇ涔熻冭檻浣跨敤鎸囬拡鎺ユ敹鑰
– 涓鑷存э細濡傛湁鎸囬拡鎺ユ敹鑰咃紝 鏈濂介兘鏄寚閽堟帴鏀惰
– 鍊兼帴鏀惰呮槸go璇█鐗规湁
– 鍊/鎸囬拡鎺ユ敹鑰呭潎鍙帴鏀跺/鎸囬拡

灏佽
– 鍚嶅瓧涓鑸娇鐢–amelCase
– 棣栧瓧姣嶅ぇ鍐:public
– 棣栧瓧姣嶅皬鍐欙細private


– 姣忎釜鐩綍涓涓寘
– main鍖呭寘鍚彲鎵ц鍏ュ彛
– 涓虹粨鏋勫畾涔夌殑鏂规硶蹇呴』鏀惧湪鍚屼竴涓寘鍐
– 鍙互鏄笉鍚屾枃浠

鎷撳睍鎷撳睍绯荤粺绫诲瀷鎴栬呭埆浜虹殑绫诲瀷
– 瀹氫箟鍒悕
– 浣跨敤缁勫悎

GOPATH鐜鍙橀噺
– 榛樿鍦▇/go
– 瀹樻柟鎺ㄨ崘:鎵鏈夐」鐩拰绗笁鏂瑰簱閮芥斁鍦ㄥ悓涓涓猤opath涓
– 涔熷彲浠ュ皢姣忎釜椤圭洰鏀惧湪涓嶅悓鐨刧opath

go get 鑾峰彇绗笁鏂瑰簱
go build 鏉ョ紪璇
go install 浜х敓pkg鏂囦欢鍜屽彲鎵ц鏂囦欢
go run 鐩存帴缂栬瘧杩愯

鎺ュ彛

type Traversal interface{
    Traverse()
}

duck typing
– “鍍忛腑瀛愯蛋璺紝鍍忛腑瀛愬彨, 閭d箞灏辨槸楦瓙”
– 鎻忚堪浜嬬墿鐨勫閮ㄨ涓鸿岄潪鍐呴儴缁撴瀯
– 涓ユ牸璇磄o灞炰簬缁撴瀯鍖栫被鍨嬬郴缁燂紝绫讳技duck typing
– 鍙堝叿鏈塲ava鐨勭被鍨嬫鏌
– 鍚屾椂鍏锋湁python,c++鐨刣uck typing鐨勭伒娲绘

鎺ュ彛鐨勫畾涔
浣跨敤鑰 -> 瀹炵幇鑰

type Retriever interface{
    Get(source string) string
}

func download(retriever Retriever) string{
    return retriever.Get("www.imooc.com")
}

鎺ュ彛鍙橀噺閲岄潰鏈変粈涔

鏌ョ湅鎺ュ彛鍙橀噺
– 琛ㄧず浠讳綍绫诲瀷: interface{}
– type assertion
– type switch

鐗规畩鎺ュ彛
– Stringer
– Reader/Writer

鍑芥暟涓庨棴鍖
鍑芥暟寮忕紪绋媣s鍑芥暟鎸囬拡

姝g粺鐨勫嚱鏁板紡缂栫▼
– 涓嶅彲鍙樻э細涓嶈兘鏈夌姸鎬侊紝鍙湁甯搁噺鍜屽嚱鏁
– 鍑芥暟鍙兘鏈変竴涓弬鏁

闂寘
鍑芥暟浣 鍖呭惈灞閮ㄥ彉閲忓拰鑷敱鍙橀噺

python涓殑闂寘

def adder():
    sum = 0
    def f(value):
        nonlocal sum
        sum += value
        return sum
    return f

鍘熺敓鏀寔闂寘 鐢closure鏉ユ煡鐪嬮棴鍖呭唴瀹

go 璇█闂寘鐨勫簲鐢
– 鏂愭尝閭e鏁板垪
– 涓哄嚱鏁板疄鐜版帴鍙
– 浣跨敤鍑芥暟鏉ラ亶鍘嗕簩鍙夋爲
– 鏇翠负鑷劧锛屼笉闇瑕佷慨楗板浣曡闂嚜鐢卞彉閲
– 娌℃湁lambda琛ㄨ揪寮忥紝鏈夊尶鍚嶅嚱鏁

7 defer璋冪敤
璧勬簮绠$悊涓庡嚭閿欏鐞

浣曟椂浣跨敤defer璋冪敤
open/close
lock/unlock
printheader/printfooter

閿欒澶勭悊

file, err := os.Open("abc.txt")
if err != nil {
    if  pathError, ok := err.(*os.PathError); ok{
        fmt.Println(pathError.Err)
    } else {
        fmt.Println("unknown error", err)
    }
}

recover
– 浠呭湪defer璋冪敤涓娇鐢
– 鑾峰彇panic鐨勫
– 濡傛灉鏃犳硶澶勭悊锛屽彲閲嶆柊panic

error vs panic
– 鎰忔枡涔嬩腑鐨勶細浣跨敤error銆 濡傛枃浠舵墦涓嶅紑
– 鎰忔枡涔嬪锛氫娇鐢╬anic 銆傚鏁扮粍瓒婄晫
閿欒澶勭悊缁煎悎绀轰緥
– defer+panic+recover

娴嬭瘯

All posts

Other pages

鍙戣〃璇勮

閭鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢*鏍囨敞