funcmain() { s := "good bye" var p *string = &s *p = "ciao"
fmt.Printf("Here is the pointer p: %p\n", p) // prints address fmt.Printf("Here is the string *p: %s\n", *p) // prints string fmt.Printf("Here is the string s: %s\n", s) // prints same string } /*output Here is the pointer p: 0x2540820 Here is the string *p: ciao Here is the string s: ciao */
funcmain() { x := min(1, 3, 2, 0) fmt.Printf("The minimum is: %d\n", x) slice := []int{7,9,3,5,1} x = min(slice...) fmt.Printf("The minimum in the slice is: %d", x) }
funcmin(s ...int)int { iflen(s)==0 { return0 } min := s[0] for _, v := range s { if v < min { min = v } } return min } /*output The minimum is: 0 The minimum in the slice is: 1 */
一个接受变长参数的函数可以将这个参数作为其他函数的参数进行传递
1 2 3 4 5 6 7
funcF1(s ...string) { F2(s...) F3(s) }
funcF2(s ...string) { } funcF3(s []string) { }
变长参数可以作为对应类型的slice进行二次传递
如果变长参数的类型不同,有两种方式可以解决
使用结构,定义一个结构类型,假设它叫Options,用于存储所有可能的参数
1 2 3 4 5
type Options struct { par1 type1, par2 type2, ... }
funcfunction1() { fmt.Printf("In function1 at the top\n") defer function2() fmt.Printf("In function1 at the bottom!\n") }
funcfunction2() { fmt.Printf("Function2: Deferred until the end of the calling function!\n") } /*output In Function1 at the top In Function1 at the bottom! Function2: Deferred until the end of the calling function! */
funcconnectToDB() { fmt.Println("ok, connected to db") }
funcdisconnectFromDB() { fmt.Println("ok, disconnected from db") }
funcdoDBOperations() { connectToDB() fmt.Println("Defering the database disconnect.") defer disconnectFromDB() //function called here with defer fmt.Println("Doing some DB operations ...") fmt.Println("Oops! some crash or network error ...") fmt.Println("Returning from function here!") return//terminate the program // deferred function executed here just before actually returning, even if // there is a return or abnormal termination before } /*output ok, connected to db Defering the database disconnect. Doing some DB operations ... Oops! some crash or network error ... Returning from function here! ok, disconnected from db */
funcmain() { result := 0 for i := 0; i <= 10; i++ { result = fibonacci(i) fmt.Printf("fibonacci(%d) is: %d\n", i, result) } }
funcfibonacci(n int) (res int) { if n <= 1 { res = 1 } else { res = fibonacci(n-1) + fibonacci(n-2) } return } /*output fibonacci(0) is: 1 fibonacci(1) is: 1 fibonacci(2) is: 2 fibonacci(3) is: 3 fibonacci(4) is: 5 fibonacci(5) is: 8 fibonacci(6) is: 13 fibonacci(7) is: 21 fibonacci(8) is: 34 fibonacci(9) is: 55 fibonacci(10) is: 89 */
funcmain() { fmt.Printf("%d is even: is %t\n", 16, even(16)) // 16 is even: is true fmt.Printf("%d is odd: is %t\n", 17, odd(17)) // 17 is odd: is true fmt.Printf("%d is odd: is %t\n", 18, odd(18)) // 18 is odd: is false }
funceven(nr int)bool { if nr == 0 { returntrue } return odd(RevSign(nr) - 1) }
funcodd(nr int)bool { if nr == 0 { returnfalse } return even(RevSign(nr) - 1) }
funcRevSign(nr int)int { if nr < 0 { return -nr } return nr } /* 16 is even: is true 17 is odd: is true 18 is odd: is false */
funcmain() { // make an Add2 function, give it a name p2, and call it: p2 := Add2() fmt.Printf("Call Add2 for 3 gives: %v\n", p2(3)) // make a special Adder function, a gets value 2: TwoAdder := Adder(2) fmt.Printf("The result is: %v\n", TwoAdder(3)) }