go语言函数作为参数传递

go语言函数作为参数传递,目前给我的感觉几乎和C/C++一致。非常的灵活。

import "fmt"
import "time"

func goFunc1(f func()) {
        go f() 
}

func goFunc2(f func(interface{}), i interface{}) {
        go f(i)
}

func goFunc(f interface{}, args... interface{}) {
        if len(args) > 1 { 
                go f.(func(...interface{}))(args)
        } else if len(args) == 1 { 
                go f.(func(interface{}))(args[0])
        } else {
                go f.(func())()
        }   
}

func f1() {
        fmt.Println("f1 done")
}

func f2(i interface{}) {
        fmt.Println("f2 done", i)
}

func f3(args... interface{}) {
        fmt.Println("f3 done", args)
}

func main() {
        goFunc1(f1)
        goFunc2(f2, 100)
        
        goFunc(f1)
        goFunc(f2, "xxxx")
        goFunc(f3, "hello", "world", 1, 3.14)
        time.Sleep(5 * time.Second)
}

f1 done

f2 done 100

f1 done

f2 done xxxx

f3 done [[hello world 1 3.14]]

转自 http://blog.csdn.net/eclipser1987/article/details/11772539