Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | 云栖社区 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Github | Search | About

1.4. 数值类型

			

数值型

有符号:int8,int16,int32,int64 无符号:byte,uint8,uint16,uint32,uint64 byte 是 uint8 的别名。 浮点类型的值有 float32 和 float64
		
字符串:string 例如:"hello world" 或者 `hello world` Go语言的字符串的字节使用UTF-8编码标识Unicode文本。

多行字符串连接
str := "Starting part"
	+ "..."
	+ "..."
    + "Ending part"
    
str := `Starting part
	...
	...
    Ending part`
    
字符串处理的包
---------------------------------------------
string包
包括字符串的分割,替换,合并等处理;
strconv包
提供许多可以在字符串和其他类型的数据之间的转换函数。
utf-8包
主要用来查询和操作UTF-8编码的字符串或者字符切片。
unicode包
提供一些用来检查Unicode码点是否符合主要标准的函数。
		
		

1.4.1. 数组

			
[5] int {1,2,3,4,5} 
长度为5的数组,其元素值依次为:1,2,3,4,5
[5] int {1,2} 
长度为5的数组,其长度是根据初始化时指定的元素个数决定的


[5] int { 2:1,3:2,4:3} 
长度为5的数组,key:value,其元素值依次为:0,0,1,2,3。在初始化时指定了2,3,4索引中对应的值:1,2,3

[...] int {1,2,3,4,5} 
长度为5的数组,其元素值依次为:1,2,0,0,0 。在初始化时没有指定初值的元素将会赋值为其元素类型int的默认值0,string的默认值是""
[...] int {2:1,4:3} 
长度为5的数组,起元素值依次为:0,0,1,0,3。由于指定了最大索引4对应的值3,根据初始化的元素个数确定其长度为5			
			
			
		
package main

import "fmt"

func main(){

	arr :=[...] int {1,2,3,4,5}

	for index, value := range arr {
		fmt.Printf("arr[%d]=%d \n", index, value)
	}

	for index := 0; index < len(arr); index++ {
		fmt.Printf("arr[%d]=%d \n", index, arr[index])
	}

}