<5> Go Context Pass contexts

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.