實(shí)現(xiàn)類似python中裝飾器的函數(shù)
package main
import (
"fmt"
)
func main(){
//可以當(dāng)成類型復(fù)制
ss := outsay("fucking golang",ssay)
ss("yangtuo")
}
func ssay(s string){
fmt.Println("lets go say: ", s)
}
func outsay(s string,f func( string)) (func(string)){ //有點(diǎn)像python裝飾器
fmt.Println("this is outside code for add on say function")
fmt.Println("handle 參數(shù),",s)
return f
}
type typenam func(...)...用法例子
package main
import "fmt"
func main() {
chr := 'K'
chr -= ('A' - 'a') //大寫換小寫
fmt.Println(string(chr))
t := File{
name: "wanlb",
}
if t.testfunc == nil{
fmt.Println("t testfunc is null")
t.testfunc = testfunc(hello)
t.testfunc("qiushu")
}
}
type testfunc func(name string) string
func (t testfunc) say(){
fmt.Println("test")
}
type File struct {
name string
testfunc
}
func hello(name string) string{
fmt.Println("this is for test typefunc",name)
return "ok test done"
}
testfunc 類理解:像是一類結(jié)合(funcname func(name string) string),都可以將函數(shù)類換成具體有函數(shù)內(nèi)容的函數(shù),進(jìn)行調(diào)用;跟interface{}有點(diǎn)像,空接口可以轉(zhuǎn)成任何類一樣,空接口也是類的集合!