go language - the conversion between data types and type

Data type classification

A data type - basic data types

  1, integer (int, signed (int8 / 1 byte, int16 / 2 bytes, int32 / 4 bytes, int64 / 8 bytes), unsigned (uint, uint8, uint16, uint32, uint64))

    The system automatically defines the number of bits int--, a 32-bit system, for the Int32, a 64-bit system, the bit int64

    The system automatically defines the number of bits uint--, a 32-bit system, compared UInt32, a 64-bit system, the bit uint64

  2, float: a + sign bit exponent bits + bits, divided float32, float64

    Mantissa bits may be lost, resulting in the loss of accuracy is not affected by the operating system, the statement defaults to 64

    There are two display modes: scientific notation: 534e3 power equivalent to 534 * 10 3, 534e3 534/10 power is equivalent to 3, e is case-insensitive; 0 decimal front when not write

  3, Boolean: it can only be true, false, one byte

  4, string

    Character: a single character enclosed in single quotes, it is necessary to format the output,% c, otherwise the number of output character code,

       utf-8 English one byte, 3 bytes Chinese

       Storage: Char - corresponding to the code number - Binary - Storage

          Reads: Binary - corresponding to the code number - Char - read

    The string can not be modified, double quotes escaped character will be identified, does not identify the transfer backticks characters inside

  5, formatted output symbols

    % c: the value corresponding to the code value unicode

    % D: represented as decimal display

    % T: the type of value

    % Q: the value corresponding to the double quotation marks go syntax string literals

    % F: display decimal

Second, the type of data - derived data types 

  Pointers, arrays, structures, pipes, functions, sections, interfaces, map

Data type conversion

A, int / float / bool / character - converted into str, conversion with fmt.sprintf,% q show double quotes strings denomination

var(
num4 = 1
num5 = 3.44
char = 'w'
bl = true
str1 string
)
  //int-->>string
str1 =fmt.Sprintf("%d",num4)
fmt.Printf("str=%q",str1)
   // float-->>string 
str1 = fmt.Sprintf("%f",num5)
fmt.Printf("str=%q",str1)

  //str-->>sting
str1 = fmt.Sprintf("%c",char)
fmt.Printf("str=%q",str)  //bool-->>string
str1 = fmt.Sprintf("%t",bl)
fmt.Printf("str=%q",str1)

Two, str turn int / float / bool, for packet conversion function strconv

  Precautions:

  1, using the function strconv package, the first package introduced strconv, returns two values, one is the first value, the second is the error, one error, given by _ does not need to be ignored,% v- original Type Output

  2, string data type conversion type basic data can be effectively transformed into

  3, as will be transformed into hello int, not being given, direct display default value of 0

  4, as will be converted to BOOL hello, not being given, direct display default value of false

  5, ParseBool (variable name), (value, error)

  6, ParseInt (variable name, display hexadecimal (hex 10), how many bits (int8 / 16/32/64)), (the value -int64, error)

  7, ParseFloat (variable name, the number of bits flaot32 / 64), (value float64, error)

   var ( 
str2 = "to true"
B1 BOOL
Str3 = "122 333"
N1 Int64
str4 = "23.33"
N2 float64
)
// String turn bool,% v display format is the default value
B1, _ = strconv.ParseBool (str2)
FMT. printf ( "% B = V", B1)

// String turn int
N1, _ = strconv.ParseInt (str3,10,64)
fmt.Printf ( "% V = N1", N1)

// String turn a float
N2, strconv.ParseFloat = _ (str4,64)
fmt.Printf ( "% V = N2", N2)
}










 

Guess you like

Origin www.cnblogs.com/puti306/p/11407501.html