MENU

Go 学习笔记:函数

August 22, 2021 • Golang

函数

在Go语言中,至少会有一个main函数,作为执行的入口,那我们如果需要自定义函数怎么写呢?

我们先来写一个判断最大值的函数

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 11:54:01
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    a, b := 12, 34
    fmt.Println(max(a, b))
}

func max(a, b int) int {
    if a >= b {
        return a
    } else {
        return b
    }
}

当然,Go也和Python一样,支持多个函数返回值

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 12:02:59
 * @Description: 寻找数组中的最大值与最小值
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
    "math"
)

func main() {
    array := [...]int{1, 2, 3, 4, 5, 6}
    fmt.Println(max_min(array[:]))
}

func max_min(array []int) (int, int) {
    maxNum, minNum := 0, math.MaxInt32
    for _, i := range array {
        if maxNum < i {
            maxNum = i
        }
        if minNum > i {
            minNum = i
        }
    }
    return maxNum, minNum
}

当然,你也可以参考Javascript的新建函数的方法

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 12:05:04
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
    "math"
)

func main() {
    array := [...]int{1, 2, 3, 4, 5, 6}
    max_min := func(array []int) (int, int) {
        maxNum, minNum := 0, math.MaxInt32
        for _, i := range array {
            if maxNum < i {
                maxNum = i
            }
            if minNum > i {
                minNum = i
            }
        }
        return maxNum, minNum
    }
    fmt.Println(max_min(array[:]))
}

不过比较糟糕的是,这个愚蠢的语言似乎没有支持重载,尽管Python和Javascript也不支持,但是后面这俩是动态语言啊!!!你一个静态的语言不支持重载真的好么???

因为不支持重载,这也导致了math.Max()只能支持float64类型的参数
/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 12:09:32
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    a, b := 1, 2
    fmt.Println(max(a, b))
}

func max(a, b int) int {
    if a > b {
        return a
    } else {
        return b
    }
}

func max(a, b float64) float64 {
    if a > b {
        return a
    } else {
        return b
    }
}

/*
# command-line-arguments
.\main.go:16:17: cannot use a (type int) as type float64 in argument to max
.\main.go:16:17: cannot use b (type int) as type float64 in argument to max
.\main.go:27:6: max redeclared in this block
        previous declaration at .\main.go:19:20
*/

顺便一提,Go是支持回调函数的

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 12:14:35
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    process(callback)
}

func callback() {
    fmt.Println("Finished")
}
func process(function func()) {
    fmt.Println("Processing")
    function()
}

变量作用域

golang中的作用域与C语言相同

局部变量和全局变量

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 21:39:54
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

var GLOBAL int = 77

func main() {
    var a, b, c int
    a, b, c = 1, 2, 3
    fmt.Println(a, b, c, GLOBAL)
    // a, b, c 是局部变量
    // GLOBAL 是全局变量
}

还有形式参数

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-22 21:49:27
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

var a, b int = 10, 20

func main() {
    fmt.Println(1, 2)
}

func sum(a, b int) int {
    fmt.Println(a, b)
    return a + b
}
作者:NorthCity1984
出处:https://grimoire.cn/golang/go7.html
版权:本文《Go 学习笔记:函数》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任