Type conversion:
Simple :( type conversion between the digital conversion is typically)
Expression: Conversion type (conversion value)
main FUNC () { var I = 42 is F: = float64 (I) var U = uint (F) var P = String (I) // change the value fmt.Printf ( " I type T =% \ n- " , I ) fmt.Printf("f type=%T\n",f) fmt.Printf("u type=%T\n",u) fmt.Printf("p type=%T\n",p) fmt.Printf("p value=%v\n",p) }
operation result:
i type=int
f type=float64
u type=uint
p type=string
p value=*
The value of p can be found has become ASCII code corresponding to 42
strconv packet conversion:
int into a string: strconv.Itoa ()
the println ( " aString " + strconv.Itoa ( 666 )) // can be directly converted, output astring666
string converted to int: strconv.Atoi ()
func Atoi(s string) (int, error)
Since the string may not be converted to int, so this function returns two values: the first value is converted into a return int value, the second value determines whether the conversion is successful return
Parse strconv package classes:
Parse function is used to convert the string class to the value (string-> x) of a given type: strconv.ParseBool (), strconv.ParseFloat (), strconv.ParseInt (), strconv.ParseUint ()
ParseBool FUNC (S String ) (I BOOL , ERR error) // truth values: 1, t, T, TRUE , true, True; False values: 0, F, F., FALSE, to false, False. FUNC parseFloat (S String , 64 ) (I float64, ERR error) // only supports 64-bit FUNC parseInt (S String , Base int , BitSize int ) (Int64 I, ERR error) // Base: binary conversion by a few, bitSize: bit converted number (8,16,32,64) FUNC ParseUint (S String , Base int , bitSize int ) (UInt64, error) // digits (8,16,32, conversion: base: according to several decimal conversion, bitSize 64)
Int string with the same turn, since the possibility can not be converted, so there are two return value, the return value is first converted into a value corresponding to the type, the second return value determines whether the conversion is successful
strconv package Format classes:
Format class function for converting a value of a string of a given type (x-> string): strconv.FormatBool (), strconv.FormatFloat (), strconv.FormatInt (), strconv.FormatUint ()
func FormatBool(i bool) string FormatFloat FUNC (I float64, FMT byte , prec, BitSize int ) String // FMT: formatting tags (b, e, E, f , g, G), prec: the digits after the decimal point represents the output accuracy, if it is < a value of 0 returns the least number of bits to represent the number, if it is larger than the number of bits corresponding to a value of 0 is returned bitSize: conversion of bits (32, 64) / *
formatting tags: ' B ' (- ddddp ± ddd, binary exponential) ' E ' (- d.dddde ± dd, decimal index) ' E ' (- d.ddddE ± dd, decimal index) ' F ' (- ddd.dddd, no index) ' G ' ( ' E ' : large indices, ' F ' : other cases) ' G ' ( ' E ' : large indices, ' F ' : other cases)
* / formatInt FUNC (i int64, Base int ) String // Base: Base conversion by a few FUNC FormatUint (i UInt64, Base int ) String // Base: press a few hex conversion
Append slice strconv slice packet based :( speaking later)
Format AppendTP class function with the same category, but converted into a string and then append a slice of: strconv.AppendBool (), strconv.AppendFloat (), strconv.AppendInt (), strconv.AppendUint (). ,
func AppendBool(i []bool) string func AppendtFloat(i []float64, fmt byte, prec, bitSize int) string AppendInt FUNC (I [] Int64, Base int ) String // Base: binary conversion by several FUNC AppendUint (I [] UInt64, Base int ) String // Base: binary conversion by several
constant:
Constant declaration and initialization of variables with similar, but var into a const, but can not use short variable
variable name = const variable type variable value
const a string = "test"