Go language primitive data types acquaintance

Basic data types commonly used, in Go are the following:

  • Integer
  • Float
  • Boolean
  • plural
  • String

Integer

  • Signed
    • int8
    • int16
    • int32
    • int64
  • Unsigned
    • uint8
    • uint16
    • uint32
    • uint64
  • Special Integer
    • uint, 32-bit operating system is uint32,64 bit operating system is uint64
    • int, 32-bit operating system is int32,64 bit operating system is int64
    • UIntPtr, unsigned integer, for storing a pointer

NOTE: use intand uintwhen the type, it can not be assumed to be 32-bit or 64-bit integer, but consider intand uintmay differ on different platforms.

Ary

// 程序入口
func main() {
    // age = "17"
    // fmt.Println("Hello World")
    // fmt.Print("isOK")
    // fmt.Printf("Age: %s", age)\n
    var a1 = 10
    fmt.Printf("十进制:%d\n", a1)
    fmt.Printf("二进制:%b\n", a1)
    fmt.Printf("八进制:%o\n", a1)
    fmt.Printf("十六进制:%x\n", a1)

    // 直接定义八进制
    a2 := 077
    fmt.Printf("直接声明八进制:%d\n", a2)
    // 直接定义十六进制
    a3 := 0x12321
    fmt.Printf("直接声明十六进制:%d\n", a3)
}

Float

Go language supports two floating-point type:

  • float32, using constants math.MaxFloat32 defined;
  • float64, using constants math.MaxFloat64 defined;

Float printing, can be used with the package fmt %f, as follows:

package main
import (
        "fmt"
        "math"
)
func main() {
        fmt.Printf("%f\n", math.Pi)
        fmt.Printf("%.2f\n", math.Pi)
}

Boolean

Go language to boolbe declared Boolean data type, Boolean data only true(真)and false(假)two values.

note:

  1. The default value of Boolean variable false.
  2. Go language is not allowed in the integer cast to Boolean.
  3. Boolean operations can not participate in value can not be converted to other types.

plural

There are a plurality of real and imaginary parts, the real and imaginary portions of complex64 32, complex128 the real and imaginary part 64. as follows:

func main(){
    var c1 complex64
    c1 = 1 + 2i
    var c2 complex128
    c2 = 2 + 3i
    fmt.Println(c1)
    fmt.Println(c2)
}

Sub-string

Go language strings to native data type appears, use the string As with other native data types (int, bool, float32, float64, etc.) the same. Go inside the string language implementation uses UTF-8code. Value is a string of 双引号(")content, may be added directly to a non-ASCII characters in the source language Go, for example:

s1 := "Joker"
s2 := "HelloWorld"

If a multi-line string is backticks, inside its contents is output, for example:

s1 := `
    你好,
    欢迎光临。
`

character

Element composition of each string is called 字符, 字符with ''enclosed, as follows:

s1 := 'H'
s2 := '你'

Go language characters in two ways:

  • uint8 type, or call type byte, representing ASCIIa sign code;
  • rune type, representative of a UTF-8symbol;

When you need to handle Chinese, Japanese or other complex characters, you need to use runetype. runeIt is actually a type int32.

Go uses a special rune type to handle Unicode, make treatment more convenient Unicode-based text, you can also use the byte type be the default string handling, performance and scalability have to take care of.

package main

import "fmt"

func main() {
    s1 := "Hello World,你好啊!"
    // fmt.Printf(s1)
    // 遍历字符串,Byte类型
    for i := 0; i < len(s1); i++ {
        fmt.Printf("%v(%c)", s1[i], s1[i])
    }
    fmt.Println()
    // 遍历字符串,rune类型
    for _, r := range s1 {
        fmt.Printf("%v(%c)", r, r)
    }
}

The output results are as follows:

72(H)101(e)108(l)108(l)111(o)32( )87(W)111(o)114(r)108(l)100(d)239(ï)188(¼)140()228(ä)189(½)160( )229(å)165(¥)189(½)229(å)149(
)138()239(ï)188(¼)129()
===============================================
72(H)101(e)108(l)108(l)111(o)32( )87(W)111(o)114(r)108(l)100(d)65292(,)20320(你)22909(好)21834(啊)65281(!)

As can be seen, the first traversal method is traversed byte, while for non-English letters or numbers, it is not occupied by a byte, such as the Chinese UTF-8encoding it occupies 3 bytes, when pressing bytes to traverse if we will get the first result, this result is not what we want. We need a second traversal, rune type used to represent UTF-8a character, a character rune by one or more bytes.

String escaping

Special string needs to maintain its original meaning, it needs to be escaped, commonly used in the following table:

Escapes meaning
\r Enter
\n Wrap
\t tabulation
\' apostrophe
\\ Backslash
\" Double quotes

such as:

package main

import "fmt"

func main() {
    s1 := "\"Hello World\""
    fmt.Printf(s1)
}

Common string operation

method effect
only (a) Seeking length (byte length)
+ Or fmt.Sprintf String concatenation
strings.Split Cutting strings
strings.contains Determining whether the character string contains
strings.HasPrefix,strings.HasSuffix Analyzing the string prefix / suffix (Boolean)
strings.Index(),strings.LastIndex() Position (index) in the output appears substring
strings.Join(a[]string, sep string) join operation

example:

package main

import "fmt"
import "strings"

func main() {
    s := "Hello World 你好啊!"
    // 求长度
    fmt.Println(len(s))
    // 字符串拼接
    s1 := "Joker"
    s2 := "你好"
    fmt.Println(s1 + s2)
    s12 := fmt.Sprintf("%s%s", s1, s2)
    fmt.Println(s12)
    // 字符串切割
    sSplit := strings.Split(s, " ")
    fmt.Println(sSplit)
    // 判断字符串是否包含
    fmt.Println(strings.Contains(s, "H"))
    fmt.Println(strings.Contains(s, "L"))
    // 判断字符串的前缀
    fmt.Println(strings.HasPrefix(s, "H"))
    // 判断字符串的后缀
    fmt.Println(strings.HasSuffix(s, "啊"))
    // 判断字串出现的位置
    fmt.Println(strings.Index(s, "H"))
    // 判断子串最后出现的位置
    fmt.Println(strings.LastIndex(s, "o"))
    // join操作
    fmt.Println(strings.Join(sSplit, "-"))
}

Modify the string

To modify the string need to first convert it into []runeor []byte, after the completion of the conversion into string. Either conversion will re-allocate memory and copy the byte array.

package main

import "fmt"

func main() {
    s1 := "hello"
    // 1、强制转换
    byteS1 := []byte(s1)
    // 2、进行修改
    byteS1[0] = 'H'
    // 3、强制转换成字符串并打印
    fmt.Println(string(byteS1))

    s2 := "我爱你中国"
    // 1、强制转换
    runeS2 := []rune(s2)
    // 2、修改
    runeS2[3] = '祖'
    // 3、强制转换成字符串并打印
    fmt.Println(string(runeS2))
}

Type Conversion

Go language only casts, no implicit type conversion. This syntax is used only when the mutual conversion between the two types of support.

Casts basic syntax is as follows:

T(表达式)

Wherein, T represents the type to be converted. Expressions include variable, operator and complex functions return values.

Guess you like

Origin blog.51cto.com/7834466/2478701