learn go return fuction

简介: package main // 参考文章: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.
package main

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

import "fmt"

func main() {
    var f = Adder()
    fmt.Print(f(1), " - ")
    fmt.Print(f(20), " - ")
    fmt.Print(f(300))
}

func Adder() func(int) int {
    var x int
    return func(delta int) int {
        x += delta
        return x
    }
}


//terminal output: 1 - 31 - 321

 

目录
相关文章
多返回值(Multiple Return Values)- 《Go语言实战指南》
Go 语言支持函数返回多个值,这一特性在实际开发中非常常见,尤其用于错误处理。本文介绍了多返回值的基本语法、用于错误处理的方式、返回值命名、忽略不需要的返回值以及实际应用场景。通过计算商和余数、文件操作、网络响应等示例,展示了多返回值的灵活性。最后总结了使用建议,包括搭配 `error` 实现显式错误控制、命名返回值提升可读性以及用 `_` 忽略无用值等技巧。
464 19
Go to Learn Go之命令行参数
Go to Learn Go之命令行参数
275 8
|
Serverless Go
Go to Learn Go之时间日期
Go to Learn Go之时间日期
316 8
Go to Learn Go之Gob
Go to Learn Go之Gob
175 8
Go to Learn Go之文件操作
Go to Learn Go之文件操作
181 8
Go to Learn Go之反射
Go to Learn Go之反射
269 8
|
存储 安全 Go
Go to Learn Go之并发
Go to Learn Go之并发
161 8
|
存储 Go
Go to Learn Go之类型转换
Go to Learn Go之类型转换
276 8
Go to Learn Go之错误处理
Go to Learn Go之错误处理
250 8
|
存储 Go
Go to Learn Go之接口
Go to Learn Go之接口
231 7

热门文章

最新文章