(golang)请教这个结构体中定义的match func(int64,int64) bool是什么意思

查看:248
本文介绍了(golang)请教这个结构体中定义的match func(int64,int64) bool是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

type IntegerFilter struct {

op    string
value int64
match func(int64, int64) bool

}

解决方案

golang中,函数本身也是个一种值类型。
另外golang是支持闭包的,也就是说可以在函数中定义函数,内层函数可以访问外层函数的变量。
运行一下下面这个例子,你就懂了。

// hello project main.go
package main

import "fmt"

func hello() {
    fmt.Println("hello")
}
func helloTo(name string) int {
    fmt.Println("hello, ", name)
    return 0
}
func main() {
    var a func()
    var b func(string) int
    a = hello
    a()
    b = helloTo
    fmt.Println(b("world"))
    b = func(name string) int {
        a()
        fmt.Println(name)
        return 1
    }
    fmt.Println(b("world"))
}

这篇关于(golang)请教这个结构体中定义的match func(int64,int64) bool是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆