• Go基础编程实践(二)—— 类型转换


    bool to string

    strconv包的FormatBool函数用于将bool转为string

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        isNew := true
        isNewStr := strconv.FormatBool(isNew)
        // message := "Purchased item is " + isNew 会报错,类型不匹配
        message := "Purchased item is " + isNewStr
        
        fmt.Println(message)
    }
    

    int/float to string

    strconv包的FormatIntFormatFloat函数用于将int、float转为string

    package main
    
    import (
         "fmt"
         "strconv"
     )
    
    func main() {
         // Int to String
         numberInt := int64(20)
         numberItoS := strconv.FormatInt(numberInt, 8)
         fmt.Println(numberItoS)
    
         // Float to String
         numberFloat := 177.12211
         // FormatFloat函数第二个参数表示格式,例如`e`表示指数格式;
         // 第三个参数表示精度,当你想要显示所有内容但又不知道确切位数可以设为-1。
         numberFtoS := strconv.FormatFloat(numberFloat, 'f', 3, 64)
         fmt.Println(numberFtoS)
    }
    

    string to bool

    strconv包的ParseBool函数用于将string转为bool

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        isNew := "true"
        isNewBool, err := strconv.ParseBool(isNew)
        if err != nil {
            fmt.Println("failed")
        } else {
            if isNewBool {
                fmt.Println("IsNew")
            } else {
                fmt.Println("Not New")
            }
        }
    }
    // ParseBool函数只接受1、0、t、f、T、F、true、false、True、False、TRUE、FALSE,其他值均返回error
    

    string to int/float

    strconv包的ParseIntParseFloat函数用于将string转为int、float

    package main
    
    import (
         "fmt"
         "strconv"
     )
    
    func main() {
         // string to int
         numberI := "2"
         numberInt, err := strconv.ParseInt(numberI, 10, 32)
         if err != nil {
             fmt.Println("Error happened")
         } else {
             if numberInt == 2 {
                 fmt.Println("Success")
             }
         }
    
        // string to float
        numberF := "2.2"
        numberFloat, err := strconv.ParseFloat(numberF, 64)
        if err != nil {
            fmt.Println("Error happened")
        } else {
            if numberFloat == 2.2 {
                fmt.Println("Success")
            }
        }
    }
    

    []byte to string

    在Go中,string的底层就是[]byte,所以之间的转换很简。

    package main
    
    import "fmt"
    
    func main() {
        helloWorld := "Hello, World"
        helloWorldByte := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100}
        fmt.Println(string(helloWorldByte), []byte(helloWorld))
        // fmt.Printf("%q", string(helloWorldByte))
    }
    

    总结

    • 转成stringFormat
    • string转其它用Parse
    • string[]byte直接转
  • 相关阅读:
    设计模式——迭代器模式
    FTP服务:FileZilla的配置和使用
    FTP服务:使用FileZilla搭建FTP服务
    FTP服务:ISS搭建服务
    javaweb项目使用RSA算法
    我在博客园的第一篇博客
    杰表打印跟乱码修改
    jsp页面角色判断
    test : 摘自 https://www.cnblogs.com/yyman001/p/3366764.html
    mybatis中sql查询不到数据单独运行sql可以获取数据
  • 原文地址:https://www.cnblogs.com/GaiHeiluKamei/p/11110846.html
Copyright © 2020-2023  润新知