<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## **1:基本介紹** 在程序開發中我們經常 將基本數據類型,轉成string,或將string **方式1:** fmt.Sprintf(“%參數”,表達式) 【推薦使用】 函數介紹: ![](https://img.kancloud.cn/c4/07/c4074725cfaabaf1859e02173af97751_419x94.png) 參數需要和表達式的類型相匹配 Fmt.Sprintf() 會返回轉換后的字符串 <br> ## **2:案例演示** 方式1:如下 將:float32,bool,byte,int,轉換成string ``` package main import ( "fmt" ) func main(){ var num int = 99 var num1 float32 = 32.12312 var num2 bool = true var num3 byte = 'a' var str string str = fmt.Sprintf("%d",num) fmt.Printf("num type is %T num = %q\n",str,str) str = fmt.Sprintf("%f",num1) fmt.Printf("num1 type is %T num1 = %q\n",str,str) str = fmt.Sprintf("%t",num2) fmt.Printf("num2 type is %T num2 = %q\n",str,str) str = fmt.Sprintf("%c",num3) fmt.Printf("num3 type is %T num3 = %q\n",str,str) } ``` **運行結果:** ``` num type is string num = "99" num1 type is string num1 = "32.123119" num2 type is string num2 = "true" num3 type is string num3 = "a" [Finished in 0.7s] ``` <br> ## **3:string轉基本數據類型(bool)如下的方法** [func FormatBool(b bool) string](https://studygolang.com/static/pkgdoc/pkg/strconv.htm#FormatBool) [func FormatInt(i int64, base int) string](https://studygolang.com/static/pkgdoc/pkg/strconv.htm#FormatInt) [func FormatUint(i uint64, base int) string](https://studygolang.com/static/pkgdoc/pkg/strconv.htm#FormatUint) [func FormatFloat(f float64, fmt byte, prec, bitSize int) string](https://studygolang.com/static/pkgdoc/pkg/strconv.htm#FormatFloat) <br> <br> ### **案例如下** #### **一:string轉基本數據類型bool** ``` package main import ( "fmt" "strconv"// 導入包 ) func main(){ // string----->轉基本數據類型(bool) var str string = "true" // 無法改變變量本身 var b bool // 聲明變量,用于接收 b,_ = strconv.ParseBool(str) fmt.Printf("b type is %T b = %v\n",b,b) } ``` 代碼說明: ``` strconv.ParseBool(str) 是有兩個返回值,一個value,另一個是error b,_=strconv.ParseBool(str) 的寫法是 _,是忽略error這個返回值 ``` **運行結果:** `b type is bool b = true` <br> #### **二:string轉基本數據類型int** ``` package main import ( "fmt" "strconv"// 導入包 ) func main(){ var str1 = "123456" var n1 int64 n1,_ = strconv.ParseInt(str1,10,64) fmt.Printf("s type is %T s = %v\n",n1,n1) } ``` <br> <br> ##### **代碼說明** ``` // 這里的ParseInt()接收三個參數,第一個是變量本身,第二個可以填0表示int,第三個是類型,32,就是int32,64就是int64 代碼的10是進制參數,寫10就是10進制,2就是2進制 ``` **運行結果:** `s type is int64 s = 123456` <br> <br> #### **三:string轉基本數據類型float** ``` var sum = "123.123" var p float64 p,_ = strconv.ParseFloat(sum,64) fmt.Printf("sum type is %T sum = %v\n",p,p) ``` **運行結果:** `sum type is float64 sum = 123.123` 注意: 1,上面的案例中,值改變了變量,并未改變變量本身 2,按照strconv.ParseInt返回值的(32,64)來接受 例如:b,n1,n2的變量數據類型 3,第18行代碼的10是進制參數,寫10就是10進制,2就是2進制 <br> <br> ## **4:注意事項** 在將String類型轉成基本數據類型時,要確保String類型能夠轉成有效的數據,比如我們可以把"123", 轉成一個整數,但是不能把hello" 轉成一個整數,如果這樣做,Golang 直接將其轉成0 ``` var ret = "hallo" var t int64 t,_ = strconv.ParseInt(ret,10,64) fmt.Printf("t type is %T t = %v\n",t,t) ``` **運行結果** `t type is int64 t = 0`
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看