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
", a, a) //string 123
   fmt.Printf("%T %d
", b,b) //int 123
   fmt.Printf("%T %d
", b2,b2) //int64 123

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

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

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

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

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



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

原文地址:https://www.cnblogs.com/haima/p/11640188.html