Those Golang features in my code-matt-Leia

Source: Internet
Author: User

-I don't use keywords

  • I don't have to Goto.

    Goto makes code execution logic difficult to read and understand.

    You can also use label tags to organize your code

    func main() {outer:    for {        log.Println("outer")        for {            log.Println("inner")            break outer        }    }}
  • I don't have to Fallthrough.

    Golang switch is good enough not to use Fallthrough to degenerate into other language patterns.

  • I don't need else

    Most of the else can be implemented with reversal judgments. For example:

     if something. OK () {something. Lock () Defer something. Unlock () Err: = something. Do () if Err = = Nil {stop: = Starttimer () defer stop () log. Println ("Working ...") doWork (something) <-something. Done ()//wait for it log. Println ("finished") return nil} else {return err}} else {return errors. New ("Something Not OK")}////* Daemon Programming *//The above code can be optimized with reversal judgments. If!something. OK () {//Flipped return errors. New ("Something Not OK")}something. Lock () Defer something. Unlock () Err: = something. Do () if err! = Nil {//flipped return Err}stop: = Starttimer () defer stop () log. Println ("Working ...") doWork (something) <-something. Done ()//wait for Itlog. Println ("finished") return nil  

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.