# Go基础之--数组和切片

+关注继续查看

## 数组

var 变量名[len] type

var a[5] int //3个整数的数组
var a[5]string //3个字符串的数组

var a=[...]int{1,2,3}

var a = [5] int{1,2,3,4,5}
var a = [5] int{1,2,3}
var a = [...]int{1,2,3,4}
var a = [5]string{1:"go",3:"python"}

var a = [3]int{1, 2, 3}
for i, v := range a {
fmt.Printf("%d %d\n", i, v)
}

var a = [3]int{1, 2, 3}
for _, v := range a {
fmt.Printf("%d\n", v)
}

var a[3][2]

var c [3][2]int
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
c[i][j] = rand.Intn(10)
}
}
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("%d ", c[i][j])
}
fmt.Println()
}

a := [2]int{1, 2}
b := [...]int{1, 2}
c := [2]int{3, 2}
d := [3]int{1, 2}
fmt.Println(a == b, a == c, b == c)
fmt.Println(a == d)

## 切片slice

slice 表示一个拥有相同类型元素的可变长的序列

var 变量名[]type
var b = []int

slice的初始化
var a[5] int //这是定义一个数组
var b[]int = a[0,2]
var b[]int = a[0:5]
var b[]int = a[:]
var b[]int = a[:3]

var b[] int = []int{1,2,3,4}

slice的底层实现就是一个数组，通常我们会叫做slice的底层数组。
slice具有三个属性：指针，长度和容量，如下图

var s = [5]int{1, 2, 3, 4, 5}
var b = s[2:3]
var c = s[0:4]

s 就好比slice的底层数组

slice创建

make([]type,len,cap)
make([]type,len)

make([]type,len)
make([]type,len,cap)

s1 := []int{1, 2, 3, 7, 8}
s2 := []int{4, 5, 6}
copy(s2, s1)
fmt.Printf("%#v\n", s2)

s1 := []int{1, 2, 3, 7, 8}
s2 := []int{4, 5, 6}
copy(s1, s2)
fmt.Printf("%#v\n", s1)

var runnes []rune
for _, v := range "hello,go" {
runnes = append(runnes, v)
}
fmt.Printf("%q\n", runnes)

s1 := []int{1, 2, 3}
s1 = append(s1, 4, 5)
fmt.Printf("%#v\n", s1)

s1 := []int{1, 2, 3}
s2 := []int{4, 5}
s1 = append(s1, s2...)
fmt.Printf("%#v\n", s1)

func appendInt(x []int, y int) []int {
var z []int
zlen := len(x) + 1
if zlen <= cap(x) {
//slice仍有增长空间扩展slice内容
z = x[:zlen]
} else {
//slice 已经没有空间，为他分配一个新的底层数组
//当然实际go底层扩展的时候的策略可能复杂的多，这里是通过扩展一倍为例子
zcap := zlen
if zcap < 2*len(x) {
zcap = 2 * len(x)
}
z = make([]int, zlen, zcap)
copy(z, x)
}
z[len(x)] = y
return z   

slice唯一允许的比较操作是和nill比较，切片的零值是nill

package main

import (
"fmt"
)

func changeString(str1 string) {
var runnes = []rune(str1)
runnes[0] = 'h'
res := string(runnes)
fmt.Println(res)
}

func main() {
changeString("Hello,Go")
}

func reverseStr(str1 string) {
var runes = []rune(str1)
var res string
for i := len(runes) - 1; i >= 0; i-- {
res += string(runes[i])
}
fmt.Println(res)
}

func reverseStr2(str1 string) {
var runes = []rune(str1)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
res := string(runes)
fmt.Println(res)
}

GO当中的：string rune,byte

rune is an alias for int32 and is equivalent to int32 in all ways. It is
used, by convention, to distinguish character values from integer values.

var a = "我爱你go"
fmt.Println(len(a))

var a = "我爱你go"
fmt.Println(len([]rune(a)))

## 时间和日期类型

time.Now().Day()

time.Now().Minute()

time.Now().Month()

time.Now().Year()

time.Duration用来表示纳秒

const (
Nanosecond Duration = 1
Microsecond =1000 * Nanosecond
Millisecond =1000 * Microsecond
Second =1000 * Millisecond
Minute =60 * Second
Hour =60 * Minute
)

fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
Format里面的时间是固定的，因为是go第一个程序的诞生时间，也不知道go的开发者怎么想的，估计是想让所有学习go的人记住这个伟大的时刻吧

Go语言核心手册-2.数组和切片

0 0
GO基本数据结构练习:数组，切片，映射

605 0

429 0
GO的数组及切片

520 0
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
- 本期是学习Golang的完结篇：函数、结构体、接口、错误入门学习，有不懂的地方可以评论进行讨论！
0 0
【Go语言刷题篇】Go从0到入门5：Map综合复习、条件语句、循环语句练习

0 0
【Go语言刷题篇】Go从0到入门4：切片的高级用法、初级复习与Map入门学习

0 0
+关注
icoders
python服务端开发，主要通信方面开发，freeswitch asterisk 最近学习go

gohbase ：HBase go客户端

Go构建日请求千亿级微服务实践

Go 构建日请求千亿级微服务的最佳实践