Go 数字转字符串,字符串转数字, 枚举

数字转字符串,字符串转数字web

package mainsvg

import (
“fmt”
“strconv”
)code

func main() {xml

// 80 转换成 "80"
//number := 80
//
//number_int :=strconv.Itoa(number)
//
//fmt.Println(number_int)
//fmt.Printf("%T", number_int)

//  若是不用这种的,可能转出来的不是你想象中的那样
//number := 80
//
//number_str := string(number)
//
//fmt.Println(number_str)  // number_str = P, 对应到了相应的ascci码上了
//fmt.Printf("%T", number_int)

// 字符串转数字 "80" 转换成 80
number := "80rrrrr"

number_int, error := strconv.Atoi(number)
if error == nil {
    fmt.Println("转换成功",number_int)
}else {
    fmt.Println("转换错误,",error)
}
//fmt.Println(error)
//fmt.Println(number_int)
//fmt.Printf("%T", number_int)

}ci

iota 枚举字符串

package mainstring

import “fmt”it

func main() {io

// 1  iota 常量×××, 每隔一行,自动累加1
// 2  iota 给常量赋值使用

const (
    a = iota //0
    b = iota  // 1
    c = iota  // 2
)

fmt.Println(a,b,c)
// 0 1 2

// 3 iota遇到const, 重置为0

const d  = iota
fmt.Println(d)

// 4 能够只写一个iota
const (
    a1 = iota
    b1
    c1
)
fmt.Println(a1, b1, c1)

// 5 若是是同一行, 值都同样

const (
    i = iota
    j1, j2, j3 = iota, iota, iota
    k = iota
)
fmt.Println(i,"\t",j1,j2,j3, "\t",k)

}import