Go Language Comma-ok Assertion

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

The syntax for the COMMA-OK assertion is: value, OK: = element. (T). element must be a variable of the interface type, and T is a normal type. If the assertion fails, OK is false, otherwise OK is true and value is the values of the variable. Let's look at an example:

Package Mainimport ("FMT") type HTML []interface{}func main () {html: = make (HTML,5) html[0] ="Div"html[1] ="Span"html[2] = []byte("Script") html[3] ="Style"html[4] ="Head"     forIndexelement: = Range HTML {if value, OK: =element. (string); OK {fmt. Printf ("html[%d] is a string and its value is%s\n", Index,value)        }Else if value, OK: =element. ([]byte); OK {fmt. Printf ("html[%d] is a []byte and its value is%s\n", Index,string(value))        }    }}

In fact, the COMMA-OK assertion also supports another way to simplify usage: value: = element. (T). But this approach is not recommended because once element is used. (T) assertion fails, a run-time error is generated. Such as:

package mainimport (    "fmt")func main() {    varinterface"good"    fmt.Println(val.(string))    // fmt.Println(val.(int))}

The line that is commented on in the above code will run as an error. This is because Val is actually storing a string type, so the assertion fails.

Another way to convert is the switch test. Since this is called a switch test, this conversion method can only appear in the switch statement. It is easy to replace the example with the COMMA-OK assertion with the switch test to achieve:

 PackageMainImport("FMT")typeHtml []Interface{}funcMain () {html: = Make(Html,5) HTML[0] ="Div"Html[1] ="Span"Html[2] = []byte("Script") HTML[3] ="Style"Html[4] ="Head"     forIndex, element: =RangeHTML {SwitchValue: = element. (type) { Case string: FMT. Printf ("html[%d] is a string and its value is%s\n", index, value) Case[]byte: FMT. Printf ("html[%d] is a []byte and its value is%s\n", Index,string(value)) Case int: FMT. Printf ("Invalid type\n")default: FMT. Printf ("Unknown type\n")        }    }}
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.