go的函数

栏目: Go · 发布时间: 5年前

内容简介:命名的函数返回值可变参数:注意:其中arg是一个slice,我们可以通过arg[index]依次访问所有参数通过len(arg)来判断传递参数的个数

命名的函数返回值

func add(a, b int) (c int) {
        c = a + b
        return
}
func calc(a, b int) (sum int, avg int) {
        sum = a + b
        avg = (a +b)/2
        return
}

可变参数:

func add(arg…int) int {    //0个或多个参数
}
func add(a int, arg…int) int {    //1个或多个参数
}
func add(a int, b int, arg…int) int {    //2个或多个参数
}

注意:其中arg是一个slice,我们可以通过arg[index]依次访问所有参数通过len(arg)来判断传递参数的个数

函数可以作为type:

package main

import "fmt"

type op_func func(int, int) int

func add(a, b int) int {
    return a + b
}

func operator(op op_func, a, b int) int {
    return op(a, b)
}

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

以上所述就是小编给大家介绍的《go的函数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning ARKit for iPhone and iPad

Beginning ARKit for iPhone and iPad

Wallace Wang / Apress / 2018-11-5 / USD 39.99

Explore how to use ARKit to create iOS apps and learn the basics of augmented reality while diving into ARKit specific topics. This book reveals how augmented reality allows you to view the screen on ......一起来看看 《Beginning ARKit for iPhone and iPad》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具