Go interface type

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

Look at the source code of Beego today to see such a code

Func Parsebool (Val interface{}) (value bool, err error) {if val! = nil {Switch V: = val. (type) {case Bool:return V, Nilcas E String:switch v {case ' 1 ', ' t ', ' t ', ' true ', ' true ', ' true ', ' yes ', ' yes ', ' yes ', ' y ', ' y ', ' on ', ' on ', ' on ', ' on ', ' on ': return True  , nilcase "0", "F", "F", "false", "false", "false", "no", "no", "no", "N", "N", "Off", "off", "Off": return False, Nil}case Int8, Int32, INT64:STRV: = Fmt. Sprintf ("%s", v) if STRV = = "1" {return true, nil} else if Strv = = "0" {return false, nil}case float64:if v = = 1 {return TR UE, NIL} else if V = = 0 {return false, Nil}}return false, FMT. Errorf ("Parsing%q:invalid Syntax", Val)}return false, FMT. Errorf ("parsing <nil>: Invalid Syntax")}
Later I was a little puzzled. This interface type can only be assigned to a variable and then come out with it.

Package Mainimport ("FMT") func main () {//test. Run () test (1)}func Test (Val interface{}) {if val! = Nil {V: = val. (type) FMT. Println (v)}}


directly to the error. Probably mean if you use this. (type) must be placed in the switch.

It seems that my go road is still very long. Play slowly.



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.