Local scope and variable derivation of Golang

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

Many people like the Golang interface, which can be implemented without declaring it; but I prefer Golang's variable derivation and if expression, and look at the following piece of code:

var ok bool var vhost *srsvhost if Vid,err: = Bravo_json_get_number (Stream, "vhost"); Err! = Nil {return err} else if Vhost,ok = Vhosts[vid];!ok {retur n Errors. New (FMT. Sprintf ("Vhost id=%v not Found", vid))} else {fmt. Println ("Vid is", vid, ", Vhost is", Vhost)} var sid string if App,err: = BR Avo_json_get_string (Stream, "app"); Err! = Nil {return err} else if sname,err: = Bravo_json_get_string (Stream, "name");                     Err! = Nil {return err} else {//all streams maps to default vhost. Sid = Fmt. Sprintf ("__defaultvhost__/%s/%s", App, Sname)} fmt. PRINTLN ("Sid is", sid, ", Vhost is", Vhost)

This place, in fact, should also have vid/app/sname three local variables, using Golang variable derivation and if expression, are limited to a smaller scope.

The variable is reduced, the scope is limited, and the code is read better.

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.