1. 程式人生 > >Go語言學習筆記(七)

Go語言學習筆記(七)

slice def 錯誤 ice switch a+b pan top 其他

13.函數

註意:有些時候我會省略main函數

  1: func functionnanme([parametername type]) [returntype] {
  2: 	//function body
  3: } // 其中參數列表和返回值列表是可選

兩個數相加

  1: func add(a int, b int) int {
  2: 	fmt.Println(a+b)
  3: 	return 0
  4: }
  5: func main() {
  6: 	add(2, 3)
  7: }

多返回值

  1: func calc(a, b int)(int, int) {
  2: 	sum:=a+b
  3: 	sub:=a-b
  4: 	return sum,sub
  5: }
  6: func main() {
  7: 	sum,sub:=calc(2,3)
  8: 	fmt.Println(sum, sub)
  9: }

換種寫法

  1: func calc(a, b int)(sum int, sub int) {
  2: 	sum=a+b
  3: 	sub=a-b
  4: 	return 
  5: }

可變參數(可以傳參數,可以不傳,也可以傳0個或多個參數)

  1: func calc_v1(b ...int) int {
  2: 	sum:=0
  3: 	for i:=0;i<len(b);i++ {
  4: 		sum = sum + b[i]
  5: 	}
  6: 	return sum
  7: }
  8: func main(){
  9: 	sum:=calc_v1()
 10: 	fmt.Printf("sum=%d\n", sum)
 11: }

defer語句

  1: func calc_v1(b ...int)(sum int, sub int) {
  2: 	defer fmt.Println("defer")
  3: 	return
  4: }

幾個例子:

  1: func test_defer() {
  2: 	defer fmt.Println("hello
")
  3: 	fmt.Println("alex")
  4: }

輸出結果

  1: >>> alex
  2: >>> hello 

多個defer 遵循棧的特性:先進後出

  1: func test_defer() {
  2: 	defer fmt.Println("hello_1")
  3: 	defer fmt.Println("hello_2")
  4: 	defer fmt.Println("hello_3")
  5: 	defer fmt.Println("hello_4")
  6: }

輸出結果

  1: >>> hello_4
  2: >>> hello_3
  3: >>> hello_2
  4: >>> hello_1
  1: func test_defer_2() {
  2: 	for i:=0;i<5;i++ {
  3: 		fmt.Println("hello%d\n", i)
  4: 	}
  5: 	fmt.Println("start...\n")
  6: }

打印結果

  1: >>> start...
  2: >>> hello,3
  3: >>> hello,2
  4: >>> hello,1
  5: >>> hello,0

  1: func test_defer_3() {
  2: 	var i int =0
  3: 	defer fmt.Printf("defer i=%d\n", i)
  4: 	i = 1000
  5: 	fmt.Printf("i=%d\n", i)
  6: }

輸出

  1: >>> i=1000
  2: >>> defer i=0

常用內置函數:

  1: close:主要用來關閉channel
  2: len:用來求長度,比如string,array,slice,map,channel
  3: new:用來分配內存,主要用來分配值類型,比如int,struct。返回的是指針
  4: make:用來分配內存,主要用來分配引用類型,比如chan,map,slice
  5: append:用來追加元素到數組,slice中
  6: panic和recover:用來做錯誤處理

幾個小練習:

質數判斷:

1.求1到100之內的所有質數,並打印到屏幕上

  1: func justfy(n int) bool {
  2: 	for i:=2;i<n;i++ {
  3: 		if n%i == 0 {
  4: 			return false
  5: 		}
  6: 	}
  7: 	return true
  8: }
  9: func problem1 () {
 10: 	for i:=2;i<100;i++ {
 11: 		if justfy(i) == true {
 12: 			fmt.Printf("%d is prime\n", i)
 13: 		}
 14: 	}
 15: }

2.打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。例如:153是一個"水仙花數",因為153=1的三次方+5的三次方+3的三次方。求100到1000之間的所有水仙花數。

  1: func is_narcissus(n int) bool {
  2: 	first := n%10
  3: 	second := (n/10)%10
  4: 	third := (n/100)%10
  5: 	sum:= first*first*first + second*second*second + third*third*third
  6: 	if sum == n{
  7: 		return true
  8: 	}
  9: 	return false
 10: }
 11: func number_array() {
 12: 	for i:=100;i<=1000;i++{
 13: 		if  is_narcissus(i) {
 14: 			fmt.Printf("%d is narcissus\n", i)
 15: 		}
 16: 	}
 17: }

3.輸入一行字符,分別統計出其中英文字母,空格,數字和其他字符的個數。

  1: func statistics_str (str string) (){
  2: 	utfChars := []rune(str)
  3: 	countChar,countNum,countSpace,countOther := 0, 0, 0, 0
  4: 	for i:=0;i<len(utfChars);i++ {
  5: 		a := utfChars[i]
  6: 		switch  {
  7: 		case a >= ‘a‘ &&  a <= ‘z‘ || a >= ‘A‘ &&  a <= ‘Z‘:
  8: 			countChar++
  9: 		case a == ‘ ‘:
 10: 			countSpace++
 11: 		case a >= ‘0‘ && a <= ‘9‘:
 12: 			countNum++
 13: 		default:
 14: 			countOther++
 15: 		}
 16: 	}
 17: 	fmt.Printf("countChar=%d\ncountNum=%d\ncountSpace=%d\ncountOther=%d\n", countChar, countNum, countSpace, countOther)
 18: }

Go語言學習筆記(七)