Go函数类型的使用

he&远方 作品

人永远都无法知道自己该要什么,因为人只能活一次,既不能拿它跟前世相比,也不能在来生加以修正。没有任何方法可以检验哪种抉择是好的,因为不存在任何比较。一切都是马上经历,仅此一次,不能准备。from 《不能承受的生命之轻》

引用于:http://wufazhuce.com/one/1380

函数在Go语言中应该是最频繁使用的类型了,所以把Go语言定义为函数式编程语言,个人认为也是一种正确的解释。函数在Go中既有类似JS语言的用法,也有C、JAVA这种语言的身影。

和JS语言对比,函数有以下特性:

  • 普通函数:作为一个普通方法使用。
  • 变长参数:函数可以接收可变数目的参数,这在JAVA里面也有类似用法。
  • 闭包函数:JS中常用的一种模式,便于维护一些特殊作用域的变量等用法。
  • 对象中的方法:JS中类面向对象的一种写法。

和C语言对比,函数有以下特性:

  • 多返回值:函数可以返回多个不同类型的结果。

Go自身定义的函数特性:

  • 巧用return:合理的利用return中断程序逻辑,会使代码显得更简洁易懂。
  • 作为参数传递:作为回调函数来说,更好的规范了回调函数引用的规范性。
  • 值传递:变量作为参数传递给函数时,默认是值传递,只是变量的一个拷贝,即副本。函数内部只是对变量副本的操作,不会影响到实际变量本身。

普通函数、多返回值

// 普通函数,不带参数
func generalFun() {
    fmt.Println("普通函数")
}

// 普通函数,带参数
func generalFun1(i int, str string) {
    fmt.Printf("i=%d str=%s\n", i, str)
}

// 普通函数,带参数和返回一个结果
func generalFun2(i int, str string) string {
    tempStr := strconv.Itoa(i) + str
    return tempStr
}

// 普通函数,带参数和返回多个结果,需要用括号包含所有返回类型
func generalFun3(i int, str string) (int, string) {
    tempStr := strconv.Itoa(i) + str
    tempI := i + i
    return tempI, tempStr
}

上面示例中的普通函数用法,作为对象的方法也是同样支持。

变长参数

格式:paramName ...type:可理解为函数可以接收type类型的可变参数,参数名称为paramName,在函数内部可把paramName理解为类似slice类型的参数值。可用for _, val := range paramName遍历传递进来的多个参数。

1、第一种用法:知道可变参数的类型时:

func myfun(prefix string, who ...string) {
    //获取可变参数的长度
    fmt.Println(len(who))
    for _, val := range who {
        fmt.Println(prefix, val)
    }
}


func Test_fun_Sample01(t *testing.T) {
    myfun("hello:", "Joe", "Anna", "Eileen")
}

Test_fun_Sample01 测试用例执行结果:

hello: Joe
hello: Anna
hello: Eileen

2、第二种用法:不知道可变参数的类型时:

如果一个变长参数的类型没有被指定,则可以使用默认的空接口 interface{},这样就可以接受任何类型的参数。该方案不仅可以用于长度未知的参数,还可以用于任何不确定类型的参数。可使用for-range循环以及switch结构对每个参数的类型进行判断

type Person struct {
    name string
}

func test8(values ...interface{}) {
    for _, val := range values {
        switch v := val.(type) {
        case int:
            fmt.Println("val type is int ", v)
        case float64:
            fmt.Println("val type is float ", v)
        case string:
            fmt.Println("val type is string ", v)
        case bool:
            fmt.Println("val type is bool ", v)
        case Person:
            fmt.Println("val type is Person ", v.name)
        case *Person:
            fmt.Println("val type is *Person ", v.name)
        default:
            fmt.Println("val type is unknow ", v)
        }
    }
}

func Test_test8(t *testing.T) {
    temp_int := 1
    temp_float := 5.6
    temp_string := "hello"
    temp_bool := true
    temp_person1 := &Person{name: "jack"}
    temp_person2 := Person{name: "rose"}
    var temp_float32 float32 = 6.6

    test8(temp_int, temp_float, temp_string, temp_bool, temp_person1, temp_person2, temp_float32)
}

运行结果:

val type is int 1
val type is float 5.6
val type is string hello
val type is bool true
val type is *Person jack
val type is Person rose
val type is unknow 6.6

示例中需要注意的两个地方:

  1. float默认定义的类型为float64
  2. struct要区分传递的参数是 Person 类型的结构体变量,还是 Person 类型的指针变量。

巧用return

1、第一种用法:无返回值函数,在内部任何程序执行片段使用return ,直接会跳出该函数,return下面的程序片段都不会再执行。

func fun_return() {
    for i := 0; i < 10; i++ {
        fmt.Printf("%v ", i)
        if i == 3 {
            return
        }
    }
} //0 1 2 3

2、第二种用法:有返回值函数,在内部任何程序执行片段使用return,它下面的程序片段都不会再执行,但是程序结尾必须以return结束,否则程序报错。

func fun_return() int {
    for i := 0; i < 10; i++ {
        fmt.Printf("%v ", i)
        if i == 3 {
            return
        }
    }
    return i
} //0 1 2 3

上面两个示例,执行结果一样,但是唯一不同的就是有返回值函数程序最后必须有return

值传递、引用传递

任何时候,如果要同步改变函数外声明的变量的值时,都要优先考虑用指针传递,即占用内存空间少,又执行速度快。

基本类型的参数传递

func test3(str string) {
    str = "go"
    fmt.Printf("str=%s", str)
}

func test4(str *string) {
    *str = "go"
    fmt.Printf("str=%s", *str)
}

func Test_test3(t *testing.T) {
    temp := "hello"
    test3(temp)
    fmt.Println(" temp=", temp) //str=go temp= hello

    test4(&temp)
    fmt.Println(" temp=", temp) //str=go temp= go
}

struct类型的参数传递,及对象传递

type Person struct {
    name string
}

// 传递Person struct类型的变量
func test5(p Person) {
    p.name = "go" // 此时的p为参数副本
    fmt.Printf("Person.name = %s", p.name)
}

// 传递指向Person struct类型的的变量地址
func test6(p *Person) {
    p.name = "go"
    fmt.Printf("Person.name = %s", p.name)
}

func Test_test5(t *testing.T) {
    p := Person{"hello"}
    test5(p)
    fmt.Println(" p.name=", p.name) //Person.name = go p.name= hello

    test6(&p)
    fmt.Println(" p.name=", p.name) //Person.name = go p.name= go

    p1 := new(Person)
    p1.name = "jack"
    test6(p1)
    fmt.Println(" p1.name=", p1.name) //Person.name = go p1.name= go
}

你可能感兴趣的:(Go函数类型的使用)