首页 > 其他分享 >0087-Go-闭包

0087-Go-闭包

时间:2022-10-30 08:35:10浏览次数:68  
标签:闭包 fmt Println nextInt func Go 0087

环境

  • Time 2022-08-23
  • Go 1.19

前言

说明

参考:https://gobyexample.com/closures

目标

使用 Go 语言的闭包。

示例

package main

import "fmt"

func intSeq() func() int {
    i := 0
    return func() int {
        i++
        return i
    }
}

func main() {

    // 生成一个闭包
    nextInt := intSeq()

    fmt.Println(nextInt())
    fmt.Println(nextInt())
    fmt.Println(nextInt())

    newInts := intSeq()
    fmt.Println(newInts())
}

总结

使用 Go 语言的闭包。

附录

标签:闭包,fmt,Println,nextInt,func,Go,0087
From: https://www.cnblogs.com/jiangbo4444/p/16840457.html

相关文章

  • 0088-Go-递归
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/closures目标使用Go语言的递归。递归函数packagemainimport"fmt"funcfact(nint)i......
  • 0089-Go-指针
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/pointers目标使用Go语言的指针。示例packagemainimport"fmt"funczeroval(ivalint)......
  • 0090-Go-字符串
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/strings-and-runes目标使用Go语言的字符串。字节遍历packagemainimport"fmt"funcma......
  • 0091-Go-结构体
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/structs目标使用Go语言的结构体。直接使用结构体packagemainimport"fmt"typeperson......
  • 0092-Go-方法
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/methods目标使用Go语言的方法。值方法packagemainimport"fmt"typerectstruct{......
  • 0093-Go-接口
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/interfaces目标使用Go语言的接口。定义接口typegeometryinterface{area()float64......
  • 0094-Go-结构体嵌入
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/struct-embedding目标使用Go语言的结构体嵌入。定义结构体typebasestruct{numint......
  • 0095-Go-泛型
    环境Time2022-08-24Go1.19前言说明参考:https://gobyexample.com/generics目标使用Go语言的泛型。泛型函数packagemainimport"fmt"funcMapKeys[Kcom......
  • 0075-Go-变量
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/variables目标使用Go语言变量的申明和使用变量。示例packagemainimport"fmt"funcma......
  • 0076-Go-常量
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/constants目标使用Go语言的常量。示例packagemainimport("fmt""math")co......