golang强制类型转换


package main

import (
	"fmt"
	"github.com/unknwon/com"
        "strconv"
)

func main() {
	test1()
}

func StringToInt(ctx *gin.Context) {
   a := "123"
   b := com.StrTo(a).MustInt() //string 转 int
   b2 := com.StrTo(a).MustInt64() //string 转 int
   fmt.Printf("%T %s\n", a, a) //string 123
   fmt.Printf("%T %d\n", b,b) //int 123
   fmt.Printf("%T %d\n", b2,b2) //int64 123

   var c string = "123"
   c2,_:= strconv.ParseInt(c,10,64)  //string 转 int
   fmt.Printf("%T %s\n", c, c) //string 123
   fmt.Printf("%T %d\n", c2,c2) //int64 123

   d := "33"
   d1 := com.StrTo(d).MustFloat64() //string 转 float64
   fmt.Printf("%T %s\n", d,d) //string 33
   fmt.Printf("%T %.2f\n", d1,d1) //float64 33.00

   e,_:= com.StrTo(a).Float64() //string 转 float64
   fmt.Printf("%T %.2f\n", e,e) //float64 123.00

   g:=strconv.Itoa(int(50)) //int转string
   fmt.Printf("%T %.2f\n", g,g) //string 50

   f:=strconv.FormatInt(int64(50), 10) //int64转string
   fmt.Printf("%T %.2f\n", f,f) //string 50



   //res := testSwitch(a)
   //httpext.SuccessExt(c, res)
}

posted @ 2019-10-09 10:33  HaimaBlog  阅读(1923)  评论(0编辑  收藏  举报