一码理解函数是一等公民


本文摘自网络,作者Atticus,侵删。

package main

import (
    "fmt"
    "strconv"
)

type Attach func(string) string

func (a Attach) loopTimes(n int) string {
    for i := 1; i < n; i ++ {
        a(strconv.Itoa(i))
    }
    return a(strconv.Itoa(n))
}

func newAttach() Attach {
    basic := "Begin"
    return func(s string) string {
        basic = fmt.Sprintf("%s\n%s", basic, s)
        return basic
    }
}

func main() {
    a1 := newAttach()
    fmt.Println(a1.loopTimes(10))
}

输出是:

Begin
1
2
3
4
5
6
7
8
9
10

你理解了么?


相关阅读 >>

手撸Golang 基本数据结构与算法 冒泡排序

关于Go get超时的问题

Go操作kafka

Golang到底能做什么

聊聊dubbo-Go-proxy的remotefilter

2.Golang 操作elasticsearch-7

Go 自定义error错误

一文读懂云原生 Go-zero 微服务框架

Go modules使用详解

Golang 使用 for 输出九九乘法表

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...