Go variables and basic data type 3

#### Go variables and basic data types (three) 
today to learn about the conversion of the remaining two basic data types (Boolean, string type) as well as basic data types
##### Boolean
Boolean type, also known as type bool, bool type only allows values true and false;
boolean 1 byte;
bool type suitable for *** *** logical operations, flow control is generally used:
IF conditional control statements;
for loop control statements ;
main Package 
Import ( 
    "FMT" 
    "the unsafe" 
    ) 
FUNC main () { 
    var A BOOL 
    fmt.Println (unsafe.Siaeof (A)) The memory space. 1 // 
    fmt.Println (a) // false default value to false 
}

  


##### string type
byte string. Go language character string is a string of characters in the character sequence of fixed length connected together, Go strings are connected by using a single byte UTF-8 unicode text encoding identification;
*** *** String memory space of 16 bytes;
case:
package main
import 
    "fmt"
    "unsafe"
    )
func main(){
    var str string 
    fmt.Println(unsafe.Siaeof(str)) // 16
    fmt.Println(str) // 为空字符串
}

  


Note the use of string and details
1. string uses UTF-8 encoding identified in Go unicode text, garbled no problem;
once 2. String value, the characters can not be changed, Go strings It is immutable;
two representations of the string of 3:
3.1 pairs of marks, the escape character may be identified;
3.2 backticks output string in its native form, is not an escape character, and comprising not escape newline special characters;
4. string concatenation using the +;
5. row string is too long, it is necessary to use multi-line character string;
main Package 
Import "FMT" 
FUNC main () { 
    var STR = "Yes" 
    STR [0] = 'H' // error, the string can be indexed, but no changes 
    var str1 = ` 
    #! / usr / bin / Python the env 
    #coding:. 8 UTF- 
    DEF Login (): 
        Print ( "Yes \ n-") 
        Print ( "Hello, World") 
    IF the __name__ == "__main__": 
        Login () 
    ` 
    fmt.Println (str1) 
    // string splicing 
    STR = "Hello" + "World" 
    fmt.Println (STR) 
    // multiple line, branch needs to be retained on + line; 
    STR = "Hello" + "World" + "Hello" + "World" + " the Hello "+" world "+ 
    "hello " + "world" + "hello " + "world" + "hello " + "world" + hello " + "world" + "hello " + "world" + "hello " + "world" + 
    fmt.Println(str)
}

  


The default value is ##### basic data types
previous articles already mentioned, here to sum up, a deeper impression;
in Go data type has a default value, when the program was not assigned to variables, use the default values, default values, known as zero-value;
the default value of the basic data types are as follows:
1. integer = 0;
2. float = 0;
3. string = "";
4. = BOOL to false;
package main
import "fmt" 
func main(){
    var a int //0
    var b float32 //0
    var c string //""
    var d bool  //false
    fmt.Println(a,b,c,d)
}

  


##### basic data type to
Go and Java /, unlike C, assignment requires *** *** the display conversion between different types of data variables are not automatically converted;
1. Syntax: T (v) convert the value of type of T, T is the data type such as: int32, int, float32 the like;
2. V: variable to be converted;
package main
import "fmt" 
func main(){
    var i int32 = 10
    // 将i 转换为float 
    var i1 float32 = float32(i)
    var i2 int8 = int8(i)
    var i3 int64 = int64(i) 
    fmt.Println(i,i1,i2,i3)
}

  


Basic data conversion Notes
1. Go data type conversion may be expressed from a small range -> indicates a large range; may also represent a large range -> represents a small range;
2. The value of the variable to be converted, the data type is not itself change;
3. during the conversion process, represents a large range -> represents the range of hours, the compiler does not complain if a small range exceeds the range of values (an article reference), according to the overflow processing, the results may not be correct, so to consider the conversion range;
main Package 
Import "FMT" 
FUNC main () { 
    var = 10 Int32 I 
    var = I1 float32 float32 (I) 
    // formatted output,% v represents the value to be output primitive type string, int et; 
    fmt.Printf ( "I % V =,% V = I1 \ n-", I, I1) 
    // variables are converted itself does not change the type of 
    fmt.Printf (" type I \ n-", I iS% T) 
}

  


##### basic string data type conversion
in development, we often need to convert the basic data types into a string, or string is converted to the base data
base data transfer string type Type
1. fmt.Sprintf ( "% v" , vars) (this is recommended, more flexible)
2. use the function strconv package
ACCORDING Sprintf Formats A to // the format specifier and the Resulting String The Returns. 
// Sprintf formatting parameters to generate the formatted string and returns the string according 
func Sprintf (format string, a ... interface {}) string

  


Case:
package main
import (
   "fmt"
   "strconv"
)
func main() {
   var a int = 1
   var b float64 = 1.1
   var c bool = true
   var d byte = 'a'
   var str string
   // 格式化int 类型
   str = fmt.Sprintf("%d",a)
   fmt.Printf("str type is %T, str value is %q\n",str,str)
   // 格式化float64
   str = fmt.Sprintf("%f",b)
   fmt.Printf("str type is %T, str value is %q\n",str,str)
   // 格式化bool
   str = fmt.Sprintf("%t",c)
   fmt.Printf("str type is %T, str value is %q\n",str,str)
   // 格式化byte
   str = fmt.Sprintf("%c",d)
   fmt.Printf ( "% T STR type IS, IS STR value Q% \ n-", STR, STR) 
   // A second way to use the function package strconv 
   // type int64 first parameter, the second parameter target hex 
   STR = strconv.FormatInt (Int64 (a), 10) 
   fmt.Printf ( "% T STR type iS, iS STR value Q% \ n-", STR, STR) 
   // first parameter type float64 The second parameter is the format, the third parameter is the data retention 10 small, 64 represents the first parameter is the original type float64 
   var = 111.12345678 float64 F 
   // = strconv.FormatFloat STR (F, 'F', 10, 32) // 111.1234588623 
   STR = strconv.FormatFloat (F, 'F', 10, 64) // 111.1234567800 
   fmt.Printf ( "% T STR type IS, IS STR value Q% \ n-", STR, STR) 

   STR = strconv.FormatBool (C) 
   fmt.Printf ( "% T STR type IS, IS STR value Q% \ n-", STR, STR) 
}

  


##### string type transfer basic data types
used in the function package strconv
func ParseBool(str string)(value bool,err error)
func ParseFloat(s string,bitSize int)(f float64,err error) 
func ParseFloat(s string,base int, bitSize int) (i int64,err error) 
func ParseUint(s string,b int,bitSize int)(n uint,err error)

  


Case:
main Package 

Import ( 
   "FMT" 
   "StrConv" 
) 

FUNC main () { 
   var = STR "to true" 
   var BOOL B 
   // ParseBool returns two values, value, ERR 
   // Go may be used if no value returned by the function _ ignored _ do not take up memory, acts like trash 
   // generally not recommended to ignore the error, because we know that will not go wrong here *** 
   b, _ = strconv.ParseBool (str) 
   fmt.Printf ( "b of the type % T iS, iS% B value V \ n-", B, B) 

   STR =" 123 " 
   // the first parameter is a string to be converted, and the second parameter is a string represented by binary numbers, the first three parameters to the original string type represents a number, here Int64 
   n-, _: = strconv.ParseInt (STR, 10, 64) 
   fmt.Printf ( "% T iS n-type, n-iS value V% \ n-", n-, n-) 

   STR = "12.123456789" 
   // the first parameter is a string to be converted, and the second parameter is a string represented by the floating point type,Here float64 
   // if the float 64 into 32 will cause loss of precision, reference strconv.FormatFloat ()
   f,_ := strconv.ParseFloat(str,64)
   fmt.Printf("f type is %T,f value is %v\n",f,f)
}

  


string transfer basic data types precautions
when converting a string type to the base data, to ensure that the string type that can turn into an effective data, such as: can be "11" into an integer, but can not be "aaa" is converted into an integer, if this processing, an error is returned, and returns a zero value of the target type (int = 0, 0 = a float, to false BOOL =)

------- recent synchronization update public micro channel number: "appetizer pickles "latest articles subject to public numbers, thanks to attention

Guess you like

Origin www.cnblogs.com/Mail-maomao/p/11357413.html