Go by Example:switch

Source: Internet
Author: User

The switch statement expresses the conditional judgment through a number of branches.

Package Mainimport "FMT" import "Time" Func main () {    //base switch usage    i: = 2    fmt. Print ("Write", I, "as")    switch I {case    1:        FMT. Println ("one") Case    2:        FMT. Println ("both") Case    3:        FMT. Println ("three")    }    //You can use commas to separate multiple conditions in a case. You can also use the default statement.    Execute the logical block specified by default when none of the above cases are satisfied.    switch time. Now (). Weekday () {case Time    . Saturday, time. Sunday:        FMT. Println ("It ' s The weekend")    default:        FMT. Println ("It's a weekday")    }    //When switch does not follow the expression, the function and the if/else are the same,    //Here we can see that the expression after the case is not necessarily a constant.    T: = time. Now ()    switch {case    t.hour () <:        FMT. Println ("It ' s before noon")    default:        FMT. Println ("It ' s after noon")    }}
Output

$ go Run switch.go Write 2 as Twoit ' s the Weekendit ' s before noon

Next example: Go by Example:arrays.

English original


Go by Example:switch

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.