zoukankan      html  css  js  c++  java
  • Go语言学习笔记(七)

    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
    ", 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
    ", i)
    
      4: 	}
    
      5: 	fmt.Println("start...
    ")
    
      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
    ", i)
    
      4: 	i = 1000
    
      5: 	fmt.Printf("i=%d
    ", 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
    ", 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
    ", 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
    countNum=%d
    countSpace=%d
    countOther=%d
    ", countChar, countNum, countSpace, countOther)
    
     18: }
  • 相关阅读:
    ASP.NET Core 2.0 : 四. _Layout与_ViewStart
    [ASP.NET MVC 小牛之路]04
    [ASP.NET MVC 小牛之路]03
    [ASP.NET MVC 小牛之路]02
    [ASP.NET MVC 小牛之路]01
    Ext JS 4 的类系统
    生活沉思录 via 哲理小故事(一)
    ExtJS框架基础:事件模型及其常用功能
    ExtJS初探:了解 Ext Core
    ExtJS初探:在项目中使用ExtJS
  • 原文地址:https://www.cnblogs.com/haoqirui/p/10099139.html
Copyright © 2011-2022 走看看