Go by Example: Variadic Functions

可变参数函数支持任意数量的传入参数。例如:fmt.Println 就是一个常见的可变参数函数。

package main

import "fmt"

// 这个函数可以使用任意数量的int型数作为参数
func sum(nums ...int) {
    fmt.Print(nums, " ")
    total := 0
    for _, num := range nums {
        total += num
    }
    fmt.Println(total)
}

func main() {

    // 可变参数函数支持使用常规方式使用,
    // 也支持只有一
    sum(1, 2)
    sum(1, 2, 3)

    // 如果你需要传入的参数在一个切片中,像下面一样
    // "func(slice...)"把切片打散传入
    nums := []int{1, 2, 3, 4}
    sum(nums...)
}

输出结果为:

<strong>$ go run variadic-functions.go </strong>
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10

在Go语言中函数另外一种重要的特性就是构建函数闭包。我们将在下一章节进行讲解。

下一个例子:Go by Example: Closures。

英文原文

时间: 2024-10-08 15:10:45

Go by Example: Variadic Functions的相关文章

Item 22: Use arguments to Create Variadic Functions

Item 22: Use arguments to Create Variadic FunctionsItem 21 describes a variadic average function, which can process anarbitrary number of arguments and produce their average value. Howcan we implement a variadic function of our own? The fixed-arity v

5.24 Declaring Attributes of Functions【转】

转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes of Functions In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your

variadic function _ golang

Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function package main import ( "fmt" ) func sum(nums ...int) { fmt.Println(nums, " ") total := 0 for _, num := range

Use apply to Call Functions with Different

Item 21: Use apply to Call Functions with DifferentNumbers of ArgumentsImagine that someone provides us with a function that calculates theaverage of any number of values: average(1, 2, 3); // 2 average(1); // 1 average(3, 1, 4, 1, 5, 9, 2, 6, 5); //

PhpStorm 8.0.3中文版

PhpStorm 8是一款强大的php集开发环境工具,主要用于PHP的开发和调试,称得上是PHP程序员的利器,软件支持Blade template engine, WordPress, and Drupal 8等开源程序,并拥有代码重构.智能代码补全.兼容html5等功能,小编提供的是PhpStorm8汉化版. 主要功能:智能PHP编辑器PHP代码补全.智能的重复编码检测器.PHP重构.支持Smarty和PHPDoc.支持多语言混合.Java Script 编辑器基于DOM/指定浏览器完成.代码

Parameter pack

Parameter pack C++ C++ language Templates A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). A function parameter pack is a function parameter that accepts zero or more fun

【转】C 宏

http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html See KernighanRitchie Appendix A12.3 for full details on macro expansion in ANSI C and http://gcc.gnu.org/onlinedocs/cpp/Macros.html for documentation on what gcc supports. The short ve

C++ Core Guidelines

C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very early draft. It is inkorrekt, incompleat, and pµøoorly formatted. Had it been an open source (code) project, this would have been release 0.6. Copy

Go by Example

Go is an open source programming language designed for building simple, fast, and reliable software. Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first exampleor browse the full list below. Hello Worl