生命不止,继续go go go
介绍来go中的变量和常量,今天介绍一下go中的基本类型。
可以分为四大类,现在一点点道来。
Boolean Types
布尔类型,不用过多介绍来吧,就是true和false。
Numeric Types
数值类型包括整型和浮点型:
integer types
floating point values
others
string types
A string type represents the set of string values. Its value is a sequence of bytes. Strings are immutable types that is once created, it is not possible to change the contents of a string. The predeclared string type is string.
Derived types
包括:
Array types 数组
Structure types 结构体
Union types 联合
Slice types 切片,引用类型
Interface types 接口
Map types 字典,引用类型
Channel Types 通道,引用类型
下面就slice map channel进行简要介绍。
首先需要明确,以上三者都是引用类型。而引用类型必须使用make函数创建,编译器会将make转化为目标类型专用的创建函数。
slice
声明:
var numbers []int /* a slice of unspecified size */
numbers = make([]int,5,5) /* a slice of length 5 and capacity 5*/
len() and cap() 函数:
len() :returns the elements presents in the slice
cap():returns the capacity of slice as how many elements it can be accomodate
package main
import "fmt"
func main() {
var numbers = make([]int,3,5)
printSlice(numbers)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
输出:len=3 cap=5 slice=[0 0 0]
sub-slice:
package main
import "fmt"
func main() {
/* create a slice */
numbers := []int{0,1,2,3,4,5,6,7,8}
printSlice(numbers)
/* print the original slice */
fmt.Println("numbers ==", numbers)
/* print the sub slice starting from index 1(included) to index 4(excluded)*/
fmt.Println("numbers[1:4] ==", numbers[1:4])
/* missing lower bound implies 0*/
fmt.Println("numbers[:3] ==", numbers[:3])
/* missing upper bound implies len(s)*/
fmt.Println("numbers[4:] ==", numbers[4:])
numbers1 := make([]int,0,5)
printSlice(numbers1)
/* print the sub slice starting from index 0(included) to index 2(excluded) */
number2 := numbers[:2]
printSlice(number2)
/* print the sub slice starting from index 2(included) to index 5(excluded) */
number3 := numbers[2:5]
printSlice(number3)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
输出:
len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
numbers[1:4] == [1 2 3]
numbers[:3] == [0 1 2]
numbers[4:] == [4 5 6 7 8]
len=0 cap=5 slice=[]
len=2 cap=9 slice=[0 1]
len=3 cap=7 slice=[2 3 4]
append() 和 copy()函数:
package main
import "fmt"
func main() {
var numbers []int
printSlice(numbers)
/* append allows nil slice */
numbers = append(numbers, 0)
printSlice(numbers)
/* add one element to slice*/
numbers = append(numbers, 1)
printSlice(numbers)
/* add more than one element at a time*/
numbers = append(numbers, 2,3,4)
printSlice(numbers)
/* create a slice numbers1 with double the capacity of earlier slice*/
numbers1 := make([]int, len(numbers), (cap(numbers))*2)
/* copy content of numbers to numbers1 */
copy(numbers1,numbers)
printSlice(numbers1)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
输出:
len=0 cap=0 slice=[]
len=1 cap=2 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=8 slice=[0 1 2 3 4]
len=5 cap=16 slice=[0 1 2 3 4]
map
声明:
/* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type
/* define the map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)
使用:
package main
import "fmt"
func main() {
var countryCapitalMap map[string]string
/* create a map*/
countryCapitalMap = make(map[string]string)
/* insert key-value pairs in the map*/
countryCapitalMap["France"] = "Paris"
countryCapitalMap["Italy"] = "Rome"
countryCapitalMap["Japan"] = "Tokyo"
countryCapitalMap["India"] = "New Delhi"
/* print map using keys*/
for country := range countryCapitalMap {
fmt.Println("Capital of",country,"is",countryCapitalMap[country])
}
/* test if entry is present in the map or not*/
capital, ok := countryCapitalMap["United States"]
/* if ok is true, entry is present otherwise entry is absent*/
if(ok){
fmt.Println("Capital of United States is", capital)
}else {
fmt.Println("Capital of United States is not present")
}
}
channel
Channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication)。
以后会进行详细的介绍。
自定义类型
使用关键字type定义自定义类型:
type flag byte
类型转换
go必须使用显式类型转换!!!!
a := 1
b := byte(a)
不能将非bool类型当true/false使用!!!!
a := 1
var b bool = a
错误:cannot use a (type int) as type bool in assignment
a := 1
if a{
}
错误:non-bool a (type int) used as if condition