This is a creation in Article, where the information may have evolved or changed.
There are channal,slick,map,interface,func and so on go, where func can be passed as a parameter this function is also supported by many dynamic languages.
Now look at how to use the
Import("FMT")type Dostruct {Namestring}func (This Do) Lock () {FMT.Println (This.Name+ "Lock ...")}func (This Do) UnLock () {FMT.Println (This.Name+ "Unlock ")}//Suppose you need to lock and unlockFunc Say () { Do := Do{Name:"Say"} Do.Lock () defer Do.UnLock () FMT.Println ("Say doing")}//Suppose you need to lock and unlockFunc fuck () { Do := Do{Name:"Fuck"} Do.Lock () defer Do.UnLock () FMT.Println ("Fuck doing")}//Retrofit-withcontextFunc Withdocontext (fn func ()) { Do := Do{Name:"Do"} Do.Lock () defer Do.UnLock () FN ()}func Quit () {Withdocontext (func () {FMT.Println ("quit doing")})}func Reboot () {FMT.Println ("reboot doing")}//context with return value//Retrofit-withcontextFunc withdocontextstring (fn func ()string)string{ Do := Do{Name:"Do"} Do.Lock () defer Do.UnLock ()returnFN ()}func main () {FMT.Println ("Start Main func ()") Say () Fuck ()//After retrofitQuit () Withdocontext (Reboot) msg:=Withdocontextstring (func ()string{return "Hahah"}) FMT.Println ("MSG", msg)}
Output:
...............do Unlock ~msg hahah