數字轉字符串,字符串轉數字
package main
import (
"fmt"
"strconv"
)
func main() {
// 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)
}
iota 枚舉
package main
import "fmt"
func main() {
// 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)
}