一码理解函数是一等公民


本文摘自网络,作者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

你理解了么?


相关阅读 >>

Go语言操作mysql语言基础知识

Go是动态语言还是静态语言

Golang 怎么做热更新

kubernetes学习笔记之csi external provisioner源码解析

分享十个优秀的 Go 类库

win7 Go语言如何安装

面试题:让你捉摸不透的 Go reslice

用 wasmedge 中的嵌入式 webassembly 函数扩展 Golang 应用程序

你知道为啥不再建议使用Gopath以及如何使用Go modules吗

解决Golang结构体json的时间格式化

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




打赏

取消

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

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

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

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

评论

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