Go里字符串与数字互相转换

Go里的字符串与数字互相转换主要使用strconv

数字转字符串

package main

import "strconv"

func main () {
  s := strconv.Itoa(-32)
}

字符串转数字

假定要转换为int类型

package main

import "strconv"

func main () {
  i, err := strconv.Atoi("-32")

  if err != nil {
    ...
  }
}

当然,也要注意长度,其中有一句话

The parse functions return the widest type (float64, int64, and uint64), but if the size argument specifies a narrower width the result can be > converted to that narrower type without data loss:

大意是说在转换时,如果类型设置不对,超出部分的数据将会丢失,毕竟Go是强类型语言

分享

TITLE: Go里字符串与数字互相转换

LINK: https://www.qttc.net/127-golang-string-number-convert-to-each.html

NOTE: 原创内容,转载请注明出自琼台博客