一码理解函数是一等公民


本文摘自网络,作者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是面向对象吗?

Golang通用链接池的实现

Golang 如何安装包

聊聊dubbo-Go-proxy的route

Golang如何做一个服务器?

聊聊dubbo-Go-proxy的accesslogfilter

Golang编译为什么快

Go modules使用详解

Golang到底能做什么

Golang中的int占几个字节

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




打赏

取消

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

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

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

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

评论

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