learn go passing variable-length arguments

简介: package main // 参考文档: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.3.md import "fmt" func main() { x := min(1, 3, 2, 0) fmt.Printf("The mininum is: %d\n", x) // 参数被存储在一个数组 arr 中,则可以通过 arr... 的形式来传递参数调用变参函数。
package main

// 参考文档:
//     https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.3.md

import "fmt"

func main() {
    x := min(1, 3, 2, 0)
    fmt.Printf("The mininum is: %d\n", x)

    // 参数被存储在一个数组 arr 中,则可以通过 arr... 的形式来传递参数调用变参函数。
    arr := []int{7, 9, 3, 5, 1}
    x = min(arr...)
    fmt.Printf("The mininum in the arr is: %d\n", x)
}

func min(a ...int) int {
    if len(a) == 0 {
        return 0
    }

    min := a[0]
    for _, v := range a {
        if v < min {
            min = v
        }
    }
    return min
}

 

目录
相关文章
Go to Learn Go之命令行参数
Go to Learn Go之命令行参数
140 8
|
Serverless Go
Go to Learn Go之时间日期
Go to Learn Go之时间日期
167 8
Go to Learn Go之Gob
Go to Learn Go之Gob
77 8
Go to Learn Go之文件操作
Go to Learn Go之文件操作
83 8
Go to Learn Go之反射
Go to Learn Go之反射
122 8
|
存储 安全 Go
Go to Learn Go之并发
Go to Learn Go之并发
78 8
|
存储 Go
Go to Learn Go之类型转换
Go to Learn Go之类型转换
148 8
Go to Learn Go之错误处理
Go to Learn Go之错误处理
120 8
|
存储 Go
Go to Learn Go之接口
Go to Learn Go之接口
139 7
|
存储 Go 索引
Go to Learn Go之映射
Go to Learn Go之映射
136 7

热门文章

最新文章