33. Toad Notes Go language--switch keywords

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

33. Toad Notes Go language--switch keywords

The switch of Go is very flexible. The expression does not have to be a constant or an integer, and the process is performed from top to bottom until a match is found, and if switch does not have an expression, it matches true. This creates a possibility--using switch to write if-else-if-else judgment sequences.

It does not match the failure automatically after the attempt is made, but you can use Fallthrough to make it do so.

Use default to specify the behavior when all other branches do not match.

code example:

Package Main

Import "FMT"

Import "Time"

Func Main () {

I: = 2

Fmt. Print ("Write", I, "as")

Switchi {

Case 1:

Fmt. Println ("one")

Case 2:

Fmt. Println ("both")

Case 3:

Fmt. Println ("three")

}

Switchtime. Now (). Weekday () {

Casetime. Saturday, time. Sunday:

Fmt. Println ("It ' sthe Weekend")

Default

Fmt. Println ("It ' sa weekday")

}

T: =time. Now ()

switch{

Caset. Hour () < 12:

Fmt. Println ("It ' sbefore Noon")

Default

Fmt. Println ("It ' safter Noon")

}

}

Output:

Write2 as

It ' sa weekday

It ' safter Noon

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.