package main
import "fmt"
+import "math/rand"
type Vector struct {
x, y uint32
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])
}
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 {
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)
+ }
}
import (
"fmt"
+ "io"
+ "log"
"math"
"math/rand"
+ "runtime"
"time"
)
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 {
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)
+}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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)
+}
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))