Golang is a big kill.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

A service has been changed to achieve with Golang, the effect is very good! The code for this service is open source, Gotasks.

Golang is one of the languages I've been exposed to, with the most efficient concurrent programming. Although node. JS is also high, but time accuracy, it is far less than Golang.

At the beginning of a lot of online tutorials, make a mess, and then re-tidy up the idea, only to find his go key word of the mystery (netizens can pit daddy a little?) )。

Why is it that his development efficiency is high, one of the main reasons is that you can completely block the way to encapsulate a function, and then through the GO keyword call, so that he at runtime concurrency, this is really scary, the closure of what the weak explosion.

Give an example of the actual point:

Func httpget (url string)   (string, error)  {    resp, err  := http. Get (URL)     if err != nil {         logger. Debug ("HttpGet",  "Request error:",  err)         return  "",  Err    }    defer resp. Body.close ()     body, err := ioutil. ReadAll (resp. Body)     if err != nil {         logger. Debug ("HttpGet",  "Io/read Error:",  err)         return  "",  err    }    return string (body), err}func  HttpPost (requrl string, post string)   (string, error)  {     Resp, err := http. Postform (requrl, Url. values{"Data":  {post})     if err != nil {         logger. Debug ("HttpPost",  "Request error:",  err)         return  "",  Err    }    defer resp. Body.close ()     body, err := ioutil. ReadAll (resp. Body)     if err != nil {         logger. Debug ("HttpPost",  "Io/read Error:",  err)         return  "",  err    }    return string (body),  err}

Assuming I have these 2 functions, httpget, httppost, execution will definitely cause blocking, for debugging convenience, blocking the way to the detection of a single function, it is certainly easier to debug, easier to monitor the function is not a bug.

But at the actual execution level, we certainly hope that he will not have any obstruction:

Func (Task *task) Start () {ch: = make (chan int) go task.request () go Task.startticker () <-ch}func (Task *t Ask) request () {logger. Log (Task. Name, "Start") Start: = time. Now () resp, _: = HttpGet (Task. URL) Complete: = time. Now () if Globalconfig.showcomplete > 0 {logger. Log (Task. Name, "Done:", complete. Sub (start), "Response content Length:", Len (RESP))} If Len (task. PostURL) > 0 {httppost (task. PostURL, RESP)}}

This code is taken from Gotasks's task_service.go,request function, which wraps a task, initiates a GET request, and, when the request returns, initiates a POST request for the contents of the get to send the content to the specified URL. The request itself is blocking execution, and in start, when calling his method, it is possible for him to execute concurrently (in effect, something like a child thread that initiates a go internal dispatch) whenever he uses go. The resource is then recycled through the channel.

As friends say, congratulations, finally get an orange outfit!

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.