zoukankan      html  css  js  c++  java
  • GO语言strconv包的使用

    Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换。

    strconv包

    strconv包实现了基本数据类型与其字符串表示的转换,官方文档中文版

    string与int类型转换

    Atoi()

    将字符串类型的整数转换为int类型。

    func Atoi(s string) (i int, err error)
    

    示例:

    func main() {
        s1 := "100"
        i1, err := strconv.Atoi(s1)
        if err != nil {
            fmt.Println("can't convert to int")
        } else {
            fmt.Printf("type:%T value:%#v
    ", i1, i1) //type:int value:100
        }
    }
    

    Itoa()

    将int类型数据转换为对应的字符串表示。

    func Itoa(i int) string
    

    示例:

    i2 := 200
    s2 := strconv.Itoa(i2)
    fmt.Printf("type:%T value:%#v
    ", s2, s2) //type:string value:"200"
    

    以上为用到最多的!
    官方文档中文版

  • 相关阅读:
    oracle学习6
    oracle学习5
    oracle学习4
    oracle学习3
    oracle的过滤与排序
    poj1064 Cable master
    poj3169 Layout
    UVA
    poj2341 Expedition
    poj3617 Best Cow Line
  • 原文地址:https://www.cnblogs.com/aresxin/p/GO-yu-yanstrconv-bao-de-shi-yong.html
Copyright © 2011-2022 走看看