$ go run string-functions.go Contains: true Count: 2 HasPrefix: true HasSuffix: true Index: 1 Join: a-b Repeat: aaaaa Replace: f00 Replace: f0o Split: [a b c d e] ToLower: test ToUpper: TEST
// 遍历嵌套 map for name, info := range users { fmt.Printf("%s: Email - %s, Phone - %s\n", name, info["email"], info["phone"]) } }
Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//格式类似于 package main import"fmt" funcplus(a int, b int)int { return a + b } funcplusPlus(a, b, c int)int { return a + b + c } funcmain() { res := plus(1, 2) fmt.Println("1+2 =", res) res = plusPlus(1, 2, 3) fmt.Println("1+2+3 =", res) }
Variadic Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package main import"fmt" funcsum(nums ...int) { fmt.Print(nums, " ") total := 0 for _, num := range nums { total += num } fmt.Println(total) } // func sum(nums ...int) 定义了一个名为 sum 的可变参数函数。
funcmain() { fmt.Println("Enter main") defer fmt.Println("Defer in main - 1") func() { fmt.Println("Enter anonymous function") defer fmt.Println("Defer in anonymous function - 1") defer fmt.Println("Defer in anonymous function - 2") fmt.Println("Exit anonymous function") }() deferOrder() defer fmt.Println("Defer in main - 2") fmt.Println("Exit main") }
funcdeferOrder() { defer fmt.Println("Defer in deferOrder - 1") defer fmt.Println("Defer in deferOrder - 2") fmt.Println("In deferOrder function") }
输出为
1 2 3 4 5 6 7 8 9 10 11
Enter main Enter anonymous function Exit anonymous function Defer in anonymous function - 2 Defer in anonymous function - 1 In deferOrder function Defer in deferOrder - 2 Defer in deferOrder - 1 Exit main Defer in main - 2 Defer in main - 1
recover:用来处理panic
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package main import"fmt" funcmayPanic() { panic("a problem") } funcmain() { deferfunc() { if r := recover(); r != nil { fmt.Println("Recovered. Error:\n", r) } }() mayPanic() fmt.Println("After mayPanic()") }
输出为
1 2 3
$ go run recover.go Recovered. Error: a problem
concurrent
goroutine
Goroutine是Go语言中最基本的并发执行单元。它是一个轻量级的”线程”,由Go运行时管理
创建一个goroutine非常简单,只需要在函数调用前加上go关键字:
1 2 3 4 5 6 7 8
funchello() { fmt.Println("Hello from goroutine!") }
select { case msg1 := <-ch1: fmt.Println("Received from ch1:", msg1) case msg2 := <-ch2: fmt.Println("Received from ch2:", msg2) case <-time.After(time.Second): fmt.Println("Timeout") }
Today, as I delved into a paper on dense shape matching and downloaded the dataset, I found myself interested in how these datasets pertaining to shape matching store correspondences between pairs of shapes. Namely, I am curious about how they preserve the ground truth of these correspondences. Then I chose SMAL dataset to conduct my research on this issue. It’s a dataset containing information on animals.