61_Go基础_1_28 字符串转换

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strconv"
 6 )
 7 
 8 func main() {
 9     /*
10         strconv包:字符串和基本类型之前的转换
11             string convert
12     */
13 
14     // fmt.Println("aa"+100)
15     // 1.bool类型
16     s1 := "true"
17     b1, err := strconv.ParseBool(s1)
18     if err != nil {
19         fmt.Println(err)
20         return
21     }
22     fmt.Printf("%T,%t\n", b1, b1) // bool,true
23 
24     ss1 := strconv.FormatBool(b1)
25     fmt.Printf("%T,%s\n", ss1, ss1) // string,true
26 
27     // 2.整数
28     s2 := "100"
29     i2, err := strconv.ParseInt(s2, 2, 64)
30     if err != nil {
31         fmt.Println(err)
32         return
33     }
34     fmt.Printf("%T,%d\n", i2, i2) // int64,4
35 
36     ss2 := strconv.FormatInt(i2, 10)
37     fmt.Printf("%T,%s\n", ss2, ss2) // string,4
38 
39     // itoa(),atoi()
40     i3, err := strconv.Atoi("-42") // 转为int类型
41     fmt.Printf("%T,%d\n", i3, i3)  // int,-42
42     ss3 := strconv.Itoa(-42)
43     fmt.Printf("%T,%s\n", ss3, ss3) // string,-42
44 }
原文地址:https://www.cnblogs.com/luwei0915/p/15629626.html