From c45ae8812750d939f259ff1693e39db48ac280cf Mon Sep 17 00:00:00 2001 From: acevest Date: Sat, 28 Nov 2015 20:21:07 +0800 Subject: [PATCH] learn go ... --- learn/go/array.slice.map.go | 74 +++++++++++++++++++++++++++++++++---- learn/go/func.go | 51 +++++++++++++++++++++++++ learn/go/pattern.go | 58 +++++++++++++++++++++++++++++ learn/go/string.go | 5 +++ 4 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 learn/go/pattern.go diff --git a/learn/go/array.slice.map.go b/learn/go/array.slice.map.go index 2c2cdeb..410fd6f 100644 --- a/learn/go/array.slice.map.go +++ b/learn/go/array.slice.map.go @@ -1,6 +1,7 @@ package main import "fmt" +import "math/rand" type Vector struct { x, y uint32 @@ -54,6 +55,10 @@ func main() { e = append(e, "append:d") e = append(e, "append:e", "append:f") + // 切片复制 + var f []string + copy(f, e) + for i := 0; i < len(e); i++ { fmt.Println(e[i]) } @@ -76,7 +81,7 @@ func main() { if Map == nil { fmt.Println("Map is nil") } - Map = make(map[string]Vector) + Map = make(map[string]Vector, 100) if Map == nil { fmt.Println("Map is nil") } else { @@ -107,21 +112,76 @@ func main() { MapA["Test"] = "USA" fmt.Println(MapA) - var s string - var ok bool - s, ok = MapA["Test"] - if !ok { + if s, ok := MapA["Test"]; !ok { fmt.Printf("Key:Test not in\n") } else { fmt.Printf("Key:Test = %s\n", s) } + for key, val := range MapA { + fmt.Printf("%-10s => %10s\n", key, val) + } + // delete delete(MapA, "Test") fmt.Println(MapA) - _, ok = MapA["Test"] - if !ok { + if _, ok := MapA["Test"]; !ok { fmt.Println("Key:Test not in") } + + AboutSlice() + AboutMap() +} + +func AboutSlice() { + var array [10]int + var slice []int = array[2:5] + + for i := 0; i < len(array); i++ { + array[i] = i + } + + for i := 0; i < len(slice); i++ { + fmt.Printf("Slice at %02d is %4d\n", i, slice[i]) + } + + fmt.Println("The Length of Array is", len(array)) + fmt.Println("The Length of Slice is", len(slice)) + fmt.Println("The Capacity of Slice is", cap(slice)) + + // grow the slice + slice = slice[:4] + for i, v := range slice { + fmt.Printf("Slice at %02d is %4d\n", i, v) + } + + fmt.Println("The Length of Slice is", len(slice)) + fmt.Println("The Capacity of Slice is", cap(slice)) + +} + +func AboutMap() { + { + // Version A + items := make([]map[int]int, 5) + for i := range items { + items[i] = make(map[int]int, 1) + items[i][rand.Intn(100)] = rand.Intn(100) + } + + fmt.Printf("Version A: Value of items: %v\n", items) + } + + { + // Version B + // Invalid Method + items := make([]map[int]int, 5) + for _, item := range items { + item = make(map[int]int, 1) + item[rand.Intn(100)] = rand.Intn(100) + } + + fmt.Printf("Version B: Value of items: %v (Invalid Method) \n", items) + } } diff --git a/learn/go/func.go b/learn/go/func.go index ac41afc..fe96067 100644 --- a/learn/go/func.go +++ b/learn/go/func.go @@ -10,8 +10,11 @@ package main import ( "fmt" + "io" + "log" "math" "math/rand" + "runtime" "time" ) @@ -37,11 +40,33 @@ func main() { fmt.Printf("Positive: %5d Negative: %5d\n", pos(i), neg(-i*i)) } + where := func() { + _, file, line, _ := runtime.Caller(1) + log.Printf("%s:%d", file, line) + } + + where() + // Fibonacci fibonacci := Fibonacci() for i := 0; i < 10; i++ { fmt.Printf("%02d - %5d\n", i, fibonacci()) } + + where() + + // Vararg Func + VarArgFuncA("Google", "Apple", "Microsoft", "Tencent") + + RecordRet("Golang") + + where() + + for i := 0; i < 10; i++ { + fmt.Printf("FibonacciRecursive(%d) is: %4d\n", i, FibonacciRecursive(i)) + } + + where() } func Adder() func(int) int { @@ -72,3 +97,29 @@ func Fibonacci() func() int { return innerfunc } + +// 变长参数函数 + +// sv 的类型为[]string +func VarArgFuncA(sv ...string) { + for i, s := range sv { + fmt.Printf("VarArgFuncA Parameter[%02d]: %s\n", i, s) + } +} + +func RecordRet(s string) (n int, err error) { + defer func() { + log.Printf("RecordRet(%q) = %d, %v", s, n, err) + }() + + return 7, io.EOF +} + +// 递归 +func FibonacciRecursive(n int) int { + if n <= 1 { + return 1 + } + + return FibonacciRecursive(n-1) + FibonacciRecursive(n-2) +} diff --git a/learn/go/pattern.go b/learn/go/pattern.go new file mode 100644 index 0000000..d1c7142 --- /dev/null +++ b/learn/go/pattern.go @@ -0,0 +1,58 @@ +/* + * ------------------------------------------------------------------------ + * File Name: pattern.go + * Author: Zhao Yanbai + * 2015-11-28 20:06:28 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ +package main + +import ( + "fmt" + "regexp" + "strconv" +) + +func main() { + defer fmt.Println("Program Exited...") + + SearchStr := "John: 2578.34 William: 4567.23 Steve: 5632.18" + Pattern := "[0-9]+.[0-9]+" + + fmt.Println(SearchStr) + + if ok, _ := regexp.Match(Pattern, []byte(SearchStr)); ok { + fmt.Println("Match Found!") + } + + re, _ := regexp.Compile(Pattern) + sa := re.ReplaceAllString(SearchStr, "##.#") + fmt.Println(sa) + + rfunc := func(s string) string { + v, _ := strconv.ParseFloat(s, 32) + + // FormatFloat(f float64, fmt byte, prec, bitSize int) string + // f: 要转换的浮点数 + // fmt: 格式标记(b、e、E、f、g、G) + // prec: 精度a(数字部分的长度,不包括指数部分) + // bitSize: 指定浮点类型(32:float32、64:float64) + // + // 格式标记 + // 'b' (-ddddp±ddd, 二进制指数) + // 'e' (-dddde±dd, 十进制指数) + // 'E' (-ddddE±dd, 十进制指数) + // 'f' (-ddd.dddd, 没有指数) + // 'g' ('e':大指数,'f':其它情况) + // 'G' ('E':大指数,'f':其它情况) + // + // 如果格式标记为 'e','E'和'f',则 prec 表示小数点后的数字位数 + // 如果格式标记为 'g','G',则 prec 表示总的数字位数(整数部分+小数部分) + + return "$" + strconv.FormatFloat(v*2, 'f', 2, 32) + } + + sb := re.ReplaceAllStringFunc(SearchStr, rfunc) + fmt.Println(sb) +} diff --git a/learn/go/string.go b/learn/go/string.go index b11352e..15798f3 100644 --- a/learn/go/string.go +++ b/learn/go/string.go @@ -38,6 +38,11 @@ func main() { str = strings.Replace(str, "example", "示例", -1) // -1表示替换所有 fmt.Println(str) + var words []rune = []rune(str) + for i, v := range words { + fmt.Printf("[%02d] : %c\n", i, v) + } + var repeat string = "Repeat " fmt.Println("Repeat:", strings.Repeat(repeat, 10)) fmt.Println("ToUpper:", strings.ToUpper(repeat)) -- 2.44.0