3、内建变量类型

一、bool

 

二、string

 

三、数字型

(u)int   // 不规定长度,在32位系统就32位,64位系统就64位

(u)int8

(u)int16

(u)int32

(u)int64  // 规定长度8、16、32、64等

unitptr  // 指针,长度跟操作系统相关

 

四、字符型

byte // 长度8位(8bit)

rune  // 长度32位

 

五、浮点型

float32

float64

complex64 

complex128  // 64、128位的复数

 

rune 相当于 go 的 char

 

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    var tempValue string = "hello看看这段话"

    // 正常输出([]byte 方法可以获得该变量所有的字节,返回slice)
    fmt.Printf("%s\n", []byte(tempValue))

    // 输出unicode编码
    for i,v := range tempValue {  // v is a rune
        fmt.Printf("(%d %x)  ",i,v)
    }
    fmt.Println()

    // 输出utf8编码(英文1字节,中文3字节)
    for _,v := range []byte(tempValue) {
        fmt.Printf("%X ",v)
    }
    fmt.Println()

    // 计算string的字符长度 (len(string) 计算的是字节数)
    fmt.Println(utf8.RuneCountInString(tempValue))

    // 简单的,循环每个字符数组
    bytes := []byte(tempValue)
    for len(bytes) > 0 {
        ch,size := utf8.DecodeRune(bytes)
        bytes = bytes[size:]
        fmt.Printf("%c ",ch)
    }
    fmt.Println()

    // 高级的,将字符串转rune ([]rune 将字符转换为rune后再返回slice)
    for i,ch := range []rune(tempValue) {
        fmt.Printf("(%d %c)",i,ch)
    }
    fmt.Println()
}

 

posted @ 2021-11-25 20:43  JaydenQiu  阅读(37)  评论(0)    收藏  举报