1、可变参数

  示例代码如下:

 package main

 import "fmt"

 // Here's a function that will take an arbitrary number
// of `ints` as arguments.
func sum(nums ...int) {
fmt.Print(nums, " ")
total :=
for _, num := range nums {
total += num
}
fmt.Println(total)
} func main() { // Variadic functions can be called in the usual way
// with individual arguments.
sum(, )
sum(, , ) // If you already have multiple args in a slice,
// apply them to a variadic function using
// `func(slice...)` like this.
nums := []int{, , , }
sum(nums...)
}

  执行上面代码,将得到以下输出结果

 [ ]
[ ]
[ ]

2、匿名函数

  示例代码如下:

 package main

 import "fmt"

 // This function `intSeq` returns another function, which
// we define anonymously in the body of `intSeq`. The
// returned function _closes over_ the variable `i` to
// form a closure.
func intSeq() func() int {
i :=
return func() int {
i +=
return i
}
} func main() { // We call `intSeq`, assigning the result (a function)
// to `nextInt`. This function value captures its
// own `i` value, which will be updated each time
// we call `nextInt`.
nextInt := intSeq() // See the effect of the closure by calling `nextInt`
// a few times.
fmt.Println(nextInt())
fmt.Println(nextInt())
fmt.Println(nextInt()) // To confirm that the state is unique to that
// particular function, create and test a new one.
newInts := intSeq()
fmt.Println(newInts())
}

  执行上面代码,将得到以下输出结果


3、递归函数

  示例代码如下:

 package main

 import "fmt"

 // This `fact` function calls itself until it reaches the
// base case of `fact(0)`.
func fact(n int) int {
if n == {
return
}
return n * fact(n-)
} func main() {
fmt.Println(fact())
}

  这个fact()函数实际上是调用它自己本身,直到它达到fact(0)时结果退出。

相关链接:

  Go可变参数的函数实例

go实例之函数的更多相关文章

  1. 函数与关系实例,函数运算与SQL,试验与关系元组

    函数是一个集合,它的每个元素都是二元组或多元组.例如 f = { (x, y) | x∈R & y∈R & y = 2x } ,g = { (x, y, z) | (x, y, z)∈ ...

  2. TypeScript基础类型,类实例和函数类型声明

    TypeScript(TS)是微软研发的编程语言,是JavaScript的超集,也就是在JavaScript的基础上添加了一些特性.其中之一就是类型声明. 一.基础类型 TS的基础类型有 Boolea ...

  3. 实例-sprintf() 函数详解-输出格式转换函数

    Part1:实例 $filterfile = basename(PHP_SELF, '.php'); if (isset($_GET['uselastfilter']) && isse ...

  4. C++ enable_if 模板特化实例(函数返回值特化、函数参数特化、模板参数特化、模板重载)

    1. enable_if 原理 关于 enable_if 原理这里就不细说了,网上有很多,可以参考如下教程,这里只讲解用法实例,涵盖常规使用全部方法. 文章1 文章2 文章3 1. 所需头文件 #in ...

  5. promise核心技术 1 实例对象/函数对象

    一个程序员要在看到代码的语法同时判断数据类型 知道语法是基础  基础才能延伸功能 //一行代码 a()[0]() // a() 首先推断出a是一个函数 //a()[0] 判断a函数的返回值是一个数组 ...

  6. Matlab编程实例(3) 函数向左或向右平移N点 左移右移

    %函数移动 close all; clear all; dir=input('请输入平移方向,“1”为向左,“2”为向右'); if dir~=1&&dir~=2;%输入控制    e ...

  7. C语言实例:函数指针

    函数指针:函数指针数组的使用: 不带参数和返回值的函数指针: #include <stdio.h> #include <stdlib.h> //定义一个没有返回值也没有入口参数 ...

  8. Java字符串常见实例与函数

    字符串比较 字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 来比较两个字符串,并返回字 ...

  9. select使用实例——str_cli函数(修订版)

    我们可以使用select函数重写http://www.cnblogs.com/nufangrensheng/p/3587962.html中的str_cli函数,这样服务器进程一终止,客户就能马上得到通 ...

随机推荐

  1. ES7前端异步玩法:async/await理解

    在最新的ES7(ES2017)中提出的前端异步特性:async.await. 什么是async.await? async顾名思义是"异步"的意思,async用于声明一个函数是异步的 ...

  2. 2778:Ride to School-poj

    2778:Ride to School 总时间限制:  1000ms 内存限制:  65536kB 描述 Many graduate students of Peking University are ...

  3. JSP8

     一.EL表达式 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式.在JSP EL表达式内可以使用整型数,浮点数 ...

  4. 【转】千万不要在JS中使用连等赋值操作

    原文链接 千万不要在JS中使用连等赋值操作   目录 前言 赋值顺序? 连续赋值能拆开写么? 后记 前言 文章标题这句话原本是在国外某JavaScript规范里看到的,当时并没有引起足够的重视,直到最 ...

  5. python 生成html文件(表格)

    import pandas as pd def convert_to_html(result,title): d = {} index = 0 for t in title: d[] = result ...

  6. [转]git问题ERROR: Repository not found.的解决

    原文地址:http://blog.csdn.net/u010154424/article/details/51233966 在github中新增了一个项目,按照git的提示添加了远程仓库,但是提交的时 ...

  7. IDEA上传项目至git

    今天来分享一下从idea上传项目至coding的过程. 本文基于windows系统. idea提供了很方便的控制git的界面化操作,除了安装git和一些必要的配置之外,用到命令行的地方会非常少. 1: ...

  8. NOIP2016提高组初赛(1)

    一.选择题 6.后缀表达式,使用二叉树来求解,正常情况下的表达式a*(b+c)- d为中序遍历的二叉树. 即 若转换为后缀表达式(左右根)则为abc+*d- 14.代数字进去,多试几遍: 三.问题求解 ...

  9. Solr7使用Oracle数据源导入+中文分词

    安装目录假设为#solr_home,本文的#solr_home为apps/svr/solr 1. 在#solr_home/server/solr下新建文件夹,假设为mjd 2. 将#solr_home ...

  10. iOS开发之线程间的MachPort通信与子线程中的Notification转发

    如题,今天的博客我们就来记录一下iOS开发中使用MachPort来实现线程间的通信,然后使用该知识点来转发子线程中所发出的Notification.简单的说,MachPort的工作方式其实是将NSMa ...