The second chapter Go language data types

First, the integer type

  Divided according to the length: int8, int16, int32, int64

  Corresponding unsigned integer: uint8, uint16, uint32, uint64

  Acquiring an object with length len ()

  Ary

    Decimal:

        var int a = 10

        fmt.Printf("%d \n",a)  //10

        fmt.Printf ( "% b \ n", a) // 1010% b denotes a binary placeholder

    Octal:

        var b = 077 you

        fmt,Printf("%o \n",c)  //ff

    Hex:

        where c int = OxFF

        fmt.Printf("%x \n",c)  //ff

        fmt.Printf("%X \n",c)  //FF

  Memory Address:

    fmt.Printf ( "% p \ n", & a) // 0x00004c080 placeholder% p represents hexadecimal memory address

  Note: You can not convert between different type int addition and subtraction, it must be converted to the same type

  any type of interface can be converted

func test() {
    var a interface{}    //{}表示空的interface
    var b int = 100
    var c float32 = 1.2
    var d string = "hello"
    a = b
    fmt.Println("a=", a)    //100
    a = c
    fmt.Println("a=", a)    //1.2
    a = d
    fmt.Println("a=", a)    //hello
}

Second, the float

  Type: float32 float64 and is not defined by default 0 imprecise floating point, or may be the first 100 * 1000 * integer programming floating calculates output placeholder% f

Third, the Boolean type

  Type: True and Flase go Flase default language can not be coerced into an integer boolean Boolean values ​​can not be involved in operations, can not be converted to other types of formatting expressed by% t

Four, String type

  Definition: internal implementations appears to utf-8 native data types

  A string that represents:

      "" // output will double quotes significance escapes

      · // meaning of anti-quotation marks ignore escapes

  Common escape character string:

      \ R & lt: carriage return (return line header) \ n-: newline (skip to the next line in the same column) \ T: Tab \ ": single quote \": double quotation marks \: backslash

  String common method

+或fmt.Sprintf    //拼接字符串
(1)
c = c+c
fmt.Printf("c = %s\n",c)
(2)
c = fmt.Sprintf("%s%s",c,c)
fmt.Printf("c=%s\n",c)

strings.Split    //分割
import "strings"
func test(){
    ip := "10.10.10.1;192.168.1.1"
    ipArray := strings.Split(ip,";")
    fmt.Printf("first ip :%s\n",ipArray[0])
    fmt.Printf("second ip :%s\n",ipArray[1])
}

strings.contains    //判断是否包含
import "strings"
func test(){
    ip := "10.10.10.1;192.168.1.1"
    result := strings.Contains(ip,"10.10.10.1")
    fmt.Println (result) // returns a Boolean value 
FUNC Test () {
Import "strings"
}

strings.HasPrefix / HasSuffix // prefix and suffix Analyzing
    str := "https://www.baidu.com"
    if strings.HasPrefix(str,"http"){
        fmt.Println("str is http url")
    }else{
        fmt.Println("str is not http url")
    }
    var last bool
    last = strings.HasSuffix(str,"com")
    fmt.Printf(last)    //返回bool
}

strings.Index()/LastIndex()    //子串出现的位置
import "strings"
func test(){
    str := "https://www.baidu.com"
    index := strings.Index(str,"baidu")    //baidu第一次出现的位置
    fmt.Printf("str is index:%d\n",index)
    index = strings.LastIndex(str,"baidu") // last to appear baidu 
}
    fmt.Printf ( "Last baidu index:% D \ n-", index)

strings.Join (a [] string, sep string) // join operation, each element of the array according to a delimiter character string as a reference reconstitute 
FUNC Test () { 
    var strArr [] = String [] {String "10.10. 10.1 "," 11.11.11.2 "," 12.12.12.3 "} 
    ResultStr = strings.Join (strArr,"; ") 
    fmt.Printf (" Result =% S \ n-"mresultStr) // output result = 10.10.10.1; 11.11.11.2; 12.12.12.3 
}

Fifth, the character type

  Definition: elements called character string, characters '' represents, there uint8 (byte) type, respectively, representing a character ASCII code; Rune type, representative of a UTF-8 character

main Package 
Import "FMT" 
FUNC main () { 
    S1: = "golang" 
    C1: = 'G' 
    fmt.Println (S1, C1) // encoding Golang 71 characters 
    s2: = "Chinese" 
    C2: = 'in' @ 3 represents characters 
    fmt.Println (s2, c2) // China 20013 
    S3: = "Go language" 
    fmt.Println (len (S3)). 11 // a 1-byte characters, Chinese 3 bytes 
    for i: = 0; i <len (s3 ); i ++ {// bytes Chinese distortion 
        fmt.Printf ( "% c \ n" , s3 [i]) //% c print characters 
    } 
    for K, V: Range = {S3 / / Chinese characters can be printed 
        fmt.Printf ( "% d,% c \ n", v) // k indicates the index number of bytes 
    } 
}

  And cast utf-8 string

main FUNC () { 
    S1: = "Big" 
    bytes 1: = [] byte (S1) // will force the string into a byte array type 
    fmt.Println (bytes1) // [98 105 103] , respectively, corresponding to the respective character 
    bytes 1 [0] = 'P' 
    fmt.Println (string (bytes 1)) // [112 105 103] 
    S1 = string (bytes 1) // byte array into a string type cast 
    fmt.Println (S1) 
    S2 : = "cabbage" 
    runes2: = [] Rune (S2) 
    runes2 [0] = 'small' 
    fmt.Println (String (runes2), len (runes2)) 
} 
// only casts, can not re-assignment

  String inversion operation

//英文
func main(){
    s1 := "hello"
    byteArray := []byte(s1)    //[h e l l o]
    //方法1
    s2 := ""
    for i :=len(byteArray)-1;i>=0;i--{
        s2 = s2 + string(byteArray[i])    //byteArray[i] --> o l l e h
    }
    fmt.Println(s2)
    //方法2
    for i := 0;i<len(byteArray)/2;i++{
        byteArray[i],byteArray(len(byteArray)-1-i) = byteArray(length-1-i),byteArray[i]
    }
  fmt.Println(string(byteArray))
}

//中文
func main(){
    str := "中文"
    r []rune := []rune(str)
    for i := 0;i<len(r)/2;i ++ { 
        s [i] = tmp
        tmp := r[len(str)-i-1]
        r [len (str) -i-1] = s [i] 
    } 
    fmt.Println (string (r [i])) 
}

Six Pointer

 

Seven, Array Array

 

Eight, Slic sliced

 

Nine, Map hash table

 

Ten, Struct structure

 

 

 

 

 

 

 

XI Time and Date

  Using the time packet type is used to represent time time.Time

  Get the current time:

    now: = time.Now () to get the current time

    now := time.Now().Day()

    now := time.Now().Minute()

    now := time.Now().Month()

    now := time.Now().Year()

  format:

func main(){
    now := time.Now()
    fmt.Printf("current time:$v\n",now)
    tear := now.Year()
    month := now.Month()
    day := now.Day()
    hour := now.Hour()
    minute := now.Minute()
    send := now.Second()
    fmt.Printf("%02d-%02d-%02d-%02d-%02d-%02d", year, month, day, hour, minute, send)
}

  Timer

main FUNC () { 
    TICKER: time.Tick = (. 5 * time.Second) // each 5s wherein the function performed once 
    for I: = {Range TICKER 
        fmt.Printf ( "% V \ n-", I) 
        function () 
    } 
}

  format

main FUNC () { 
    now: = Time.now () 
    fmt.Println (now.Format ( "02/1/2006 15:04")) 
    fmt.Println (now.Format ( "2006/1/02 15:04 ")) 
    fmt.Println (now.Format (" 2006/1/02 ")) 
} 
// format can be changed, time must be 2,006,102 15:04

  Calculation program running time

func main(){
    start := time.Now().UnixNano()
    for i := 0;i<10;i++{
        time.Sleep(time.Millisecond)
    }
    end := time.Now().UnixNano()
    cost := (end - start)/1000000    //微秒化成秒
    fmt.Printf("%vn",cost)
}

  

 

Guess you like

Origin www.cnblogs.com/parallel-Y/p/11311603.html