Go core development study notes (d) - basic data types and String interchangeable

Basic data type to:
require explicit conversion (cast) the assignment between different types of variables Golang, Goalng data types can not be converted automatically!

Conversion Method: Similar python, communicated T (v) function to convert embodiment.
Examples are as follows

	package main
	import "fmt"
	
	func main() {
	//尝试使用 i = float32(i)不可行,必须要重新定义变量
	var i int = 100
	var i1 float32 = float32(i)
	fmt.Printf("i1=%v\n",i1)
	fmt.Printf("i的类型为:%T\n",i)      //int
	fmt.Printf("i1的类型为:%T",i1)      //float32
	}

Illustration:

  1. Small becomes big range scope, range of small to big range can, int8 change int16, int16 become int8 can be.

  2. Data conversion type test sites: just by converting the converted value for the specified type impart i i1, i itself and the type of data does not change;
    is converted value is stored in a variable, the variable data type itself has not changed.

  3. In the conversion, if converted to int64 int8, the compiler does not complain but the result of the conversion process will overflow in binary, the result is inconsistent, so do not turn strong data types.

  4. Two examples of a deep understanding of data types:

    // type of data conversion value assigned can not cross impassable data types, such as int32 int64 assigned to, this is definitely not, must be converted to int64 int32 variable value

    var (
    	n1 int32
    	n2 int64
    	n3 int8
    )
    n1 = 32
    n2 = int64(n1) + 10
    n3 = int8(n2) + 10
    fmt.Printf("%d %d %d",n1,n2,n3)
    

    // line with intxx type but does not meet the range of values, the compiler does not complain, but the value would overflow; up assignment does not meet the numerical range of values, the compiler directly error, then the no
    // compiler can not pass directly, n5 data overflow, n6 itself +128 can not be assigned to n6

    var (
    n4 int8
    n5 int8
    n6 int8
    )
    n4 = 10
    n5 = int8(n4) + 127
    n6 = int8(n4) + 128
    fmt.Printf("%d %d %d",n4,n5,n6)
    

★ basic data types and string types are interconvertible

  1. Program development often requires string system conversion and other types.

  2. Basic type conversion String:
    (. 1) fmt.Sprintf () : Sprintf generated character string format parameter and returns the formatted string.

    (2)strconv():
    func FormatBool(b bool) string
    func FormatInt(i int64, base int) string
    func FormatUint(i uint64, base int) string
    func FormatFloat(f float64, fmt byte, prec, bitSize int) string

    Illustrates two conversion codes ways:

    package main
    
    import (
    	"fmt"
    	"strconv"
    )
    
    func main() {
    	//第一种方式,使用fmt.Sprintf()函数来转
    	num1,num2,bo,char := 90,20.5,true,"b"
    	var str,str1,str2,str3 string
    	str = fmt.Sprintf("%d\n",num1)
    	str1 = fmt.Sprintf("%f\n",num2)
    	str2 = fmt.Sprintf("%t\n",bo)
    	str3 = fmt.Sprintf("%v\n",char)
    	fmt.Println(str,str1,str2,str3)
    
    	/*第二种方式,使用package strconv:
    	func FormatBool(b bool) string
    	func FormatInt(i int64, base int) string
    	func FormatUint(i uint64, base int) string
    	func FormatFloat(f float64, fmt byte, prec, bitSize int) string
    	 */
    	var str4,str5,str6 string
    	num3,num4,bo1 := 99,13.56,true
    	//参数1:必须为int64, 参数2:多少进制
    	str4 = strconv.FormatInt(int64(num3),10)
    	//参数1:必须为float64,参数2:f是正常输出,e则是e的n次方格式
    	//参数3:小数点后多少位,参数4:bitSize为64
    	str5 = strconv.FormatFloat(float64(num4),'f',2,64)
    	//参数1:直接传变量即可                
    	str6 = strconv.FormatBool(bo1)
    	fmt.Println(str4,str5,str6)
        
        //Itoa()函数挺好使的,直接把一个int转换成字符串,但是不支持int64,相当于num5如果不是int还需要转int
    	var num5 int = 100
    	str7 = strconv.Itoa(num5)
    	fmt.Println(str7)
    }
    

3. string data type conversion type basic
FUNC ParseBool (STR String) (BOOL value, ERR error)
FUNC parseInt (String S, Base int, int BitSize) (Int64 I, ERR error)
FUNC ParseUint (S String, int Base, BitSize int) (n-UInt64, ERR error)
FUNC parseFloat (S String, BitSize int) (F float64, ERR error)

Use strconv.Parsexxxx () function to do different types of conversions.
The following case study, which shows how a string rotation basic types:

	package main
	
	import (
	"fmt"
	"strconv"
	)
	
	func main() {
	/*
	可以看出来这里面几个函数都会返回两个值,一个是value,一个是error,我们
	需要的只是value,对error并不关心,所以可以使用"_"来忽略这个error
	例如:
	b , _ = strconv.ParseBool(str)
	func ParseBool(str string) (value bool, err error)
	func ParseInt(s string, base int, bitSize int) (i int64, err error)
	func ParseUint(s string, base int, bitSize int) (n uint64, err error)
	func ParseFloat(s string, bitSize int) (f float64, err error)
	*/
	
	//布尔值转换
	var str string = "true"
	var b bool
	b , _ = strconv.ParseBool(str)      //这个写法要注意,因为返回两个值
	fmt.Println(b)
	
	//int转换
	var str1 string = "123456"
	var num int64
	num , _  = strconv.ParseInt(str1,10,64)
	fmt.Printf("%T\n",num)
	
	//float转换
	var str2 string = "123.456"
	var num1 float32
	var num2 = float64(num1)
	num2 , _ = strconv.ParseFloat(str2,64)
	fmt.Println(num2)
	}

Published 50 original articles · won praise 18 · views 4025

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89574743