Golang Introduction and one of the series of stepping pits

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

Golang is a concise, powerful and secure programming language. I like him. Support Pointers also provide various safety mechanisms to reduce the brain degree of low-grade fever. Like what:

funcgetIntegerAddress()*int{    a:=10    return&a}

This thing put in C + + you will be wrong to want to die, but with Golang this is a normal implementation (although a bit two). And the performance of Go is not bad.

If you are a very weak and C + + programmer like me, you should be more excited to see this.

Don't worry, there are other shiny things.

Channel and Goroutine.

You make a thing, you know someone will deal with it, but you don't know who it is. Maybe you make this thing very fast, but the people who deal with it are rather slow. A less figurative metaphor is that you can pooh-pooh-spit 10 of phlegm instantly, and then clean up the aunt to curse your family and the chrysanthemums of your unborn child and use various tools to clean up the stuff. We can imagine that an aunt is not able to quickly take care of your 10 sputum, we may need to work with the N-aunt. If it is another language, we may have to create an aunt's worker pool, and like a way to represent the thick phlegm of the memeory shared out to the ladies.

If we use the Golang tool, we can write a code like this:

 Package MainImport "Time"var Phlegms =  Make(Chan int) //Here All Spit phlegmfunc Spit(Phlegm int) { //Spit functionPhlegms <- Phlegm}func Cleanitup() {//Aunt function for {Select {         Case Phlegm := <-Phlegms:     println("I cleaned", Phlegm)                }}}func Main() { for I := 0; I <  +; I++ {Go Spit(I)//Even spit 1000 sputum} for I := 0; I < Ten; I++ {Go Cleanitup()//Generate 10 aunts}<- Time. After(5 *  Time.Second)}

The same as I concurrency bitter hands, see here is not can not restrain to with Golang to come?

It's time to use a few golang pits to properly balance your enthusiasm, otherwise everyone will have to hire a few aunts to help you cleanup your screen.

func HttpHandler() {Resultch :=  Make(Chan BOOL)Go func() {defer Close(Resultch)Resultch <- Sometimeconsumingrpc()}()Select { Case result := <-Resultch:println(result) Case <- Time. After(1 *  Time.Second):println("Shit")}}

We assume that Sometimeconsumingrpc () is a less stable call, and sometimes it returns more than a second. So congratulations, your HTTP service will: memory leaks!

The reason is: Goroutine can not be forced to kill! When more than a second, no one read Resultch case, write the result of the goroutine can never be completed! Each goroutine consumes more than 8k of memory, and when you get stuck with more goroutine, your memory footprint and Goroutine total will remain high and eventually lead to your server running out of memory and service crashing.

Maybe some old wet want to jump out to say: Landlord SB. Give Resultch a buffer. Yes, this example will work, but is the buffered channel a panacea? Let's look at one more example.

 Package MainImport "FMT"func HttpHandler() {Errch :=  Make(Chan Error, 1)Resultch :=  Make(Chan int, 1)Go func() {defer Close(Errch)defer Close(Resultch)Errch <- FMT.Errorf("Shit")}()Select { Case <-Errch: Case <-Resultch:println("This shall not happen dude!")}}func Main() { for I := 0; I < 1000000; I++ {HttpHandler()}}

This example is particularly interesting. Run 1 million times, my own MBP above appeared less than 10 times "This shall not happen dude!" 's log.

Smart you, what is this for?

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.