go interface type

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

今天看beego的原始碼的時候看到這麼一段code

func ParseBool(val interface{}) (value bool, err error) {if val != nil {switch v := val.(type) {case bool:return v, nilcase string:switch v {case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "Y", "y", "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 true, 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")}
後來自己有點疑惑。這個interface type 能不能單單的賦值給一個變數然後拿出來用呢

package mainimport ("fmt")func main() {//test.Run()test(1)}func test(val interface{}) {if val != nil {v := val.(type)fmt.Println(v)}}


直接給報錯了。 大概的意思說 如果使用這個 .(type) 必須放到switch 裡面。

看來我的go路還很長。慢慢玩

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.