//切片初始化
var numbers = make([]int, 3, 5)
fmt.Printf("len=%d cap=%d slice=%v\n", len(numbers), cap(numbers), numbers)
//len=3 cap=5 slice=[0 0 0]
//切片截取
//创建切片
x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
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]
fmt.Println("numbers ==", x)
//numbers == [0 1 2 3 4 5 6 7 8]
fmt.Println("numbers[1:4] ==", x[1:4])
//numbers[1:4] == [1 2 3]
/* 默认下限为 0*/
fmt.Println("numbers[:3] ==", x[:3])
//numbers[:3] == [0 1 2]
/* 默认上限为 len(s)*/
fmt.Println("numbers[4:] ==", x[4:])
//numbers[4:] == [4 5 6 7 8]
numbers1 := make([]int,0,5)
fmt.Println("numbers ==", numbers1)
//numbers == []
number2 := x[:2]
fmt.Println("numbers ==", number2)
fmt.Printf("len=%d cap=%d slice=%v\n", len(number2), cap(number2), number2)
//len=2 cap=9 slice=[0 1]
number3 := x[2:5]
fmt.Printf("len=%d cap=%d slice=%v\n", len(number3), cap(number3), number3)
//len=3 cap=7 slice=[2 3 4]
首先判断,申请容量(cap),大于2倍的旧容量(old,cap),最终容量(newcap),就是申请的容量cap
否则判断,如果旧切片的长度小于1024,则最终的容量newcap)就是旧容量(old,cap)的两倍
如果旧切片长度大于1024,最终容量(newcap)从旧容量old,cap开始循环原来的1/4即(newcap)最终容量(cap)计算值溢出
最终申请新的容量(cap),如果小于1024扩容速度是2倍,大于1024 扩容是1.25倍
var x []int
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
x = append(x, 0)
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
x = append(x, 1)
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
//追加多个元素
x = append(x, 2, 3, 4)
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
//len=5 cap=6 slice=[0 1 2 3 4]
//创建切片
numbers1 := make([]int, len(x), cap(x)*2)
copy(numbers1, x)
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
//len=5 cap=6 slice=[0 1 2 3 4]
//定义数组
var n [10]int
var i int
for i = 0; i < 10; i++ {
n[i] = i + 100
}
fmt.Println(n)
//多维数组的定义和遍历
//多维数组
a := [3][4]int{
{0, 1, 2, 3}, /* 第一行索引为 0 */
{4, 5, 6, 7}, /* 第二行索引为 1 */
{8, 9, 10, 11}, /* 第三行索引为 2 */
}
fmt.Println(a)
site := [2][2]string{}
site[0][0] = "Google"
site[0][1] = "Hello1"
site[1][0] = "Hello2"
site[1][1] = "Hello3"
fmt.Println(site)
var i, j int
//输出二维数组
for i = 0; i < 2; i++ {
for j = 0; j < 2; j++ {
fmt.Println(site[i][j])
}
}
package main
import "fmt"
//函数多个返回值
func swap(x,y string)(string,string){
return y,x
}
//值引用
func swap2(x,y int)int {
var temp int
temp = x
x = y
y = temp
return temp
}
//引用传递
func swap3(x *int,y *int) {
var temp int
temp = *x
*x = *y
*y = temp
}
func main() {
a, b := swap("google", "hello")
fmt.Println(a, b)
var a1 = 100
var b1 = 200
//值传递
fmt.Printf("交换前 a 的值为 : %d\n", a)
fmt.Printf("交换前 b 的值为 : %d\n", b)
//交换前 a 的值为 : %!d(string=hello)
//交换前 b 的值为 : %!d(string=google)
swap2(a1, b1)
fmt.Printf("交换后 a 的值 : %d\n", a)
fmt.Printf("交换后 b 的值 : %d\n", b)
//交换后 a 的值 : %!d(string=hello)
//交换后 b 的值 : %!d(string=google)
//引用传递
var a2 = 200
var b2 = 300
swap3(&a2, &b2)
fmt.Printf("交换后,a 的值 : %d\n", a2)
fmt.Printf("交换后,b 的值 : %d\n", b2)
//声明函数变量
getFunc := func(x int) int {
return x
}
fmt.Println(getFunc(2))
nextNumber :=callback()
fmt.Println(nextNumber())
//函数作为另一个函数的参数
score := []int{10, 50, 80, 90, 100}
fmt.Println(filter(score, func(a int) bool {
if a >= 60 {
return true
}else{
return false
}
}))
}
//闭包
func callback()func()int {
i := 0
return func() int {
i += 1
return i
}
}
//函数实例
func filter(score []int, f func(int) bool)[]int {
reSlice := make([]int, 0)
for _, v := range score {
if f(v){
reSlice = append(reSlice, v)
}
}
return reSlice
}
package main
import "fmt"
type Books struct {
title string
author string
subject string
}
func main() {
//定义结构体
fmt.Println(Books{"Go语言","go","652151"})
//key=>value格式
fmt.Println(Books{title:"Go语言",author:"gogogo",subject:"652151"})
//忽略字段
fmt.Println(Books{title:"Golang语言",author:"golang"})
//访问结构体
var Book1 Books
Book1.title = "go语言学习之路"
Book1.author = "go study"
Book1.subject = "赶快来学习golang吧!"
fmt.Println(Book1)
//结构体指针
var Book2 Books
Book2.title = "go语言学习"
Book2.author = "go语言学习"
Book2.subject = "13214"
fmt.Println(&Book2)
}
var a int = 20 //声明变量
var ip *int //指针变量
ip = &a //指针变量的存储地址
fmt.Printf("a 变量的地址是: %x\n", &a )
/* 指针变量的存储地址 */
fmt.Printf("ip 变量储存的指针地址: %x\n", ip )
/* 使用指针访问值 */
fmt.Printf("*ip 变量的值: %d\n", *ip )
fmt.Printf("a 变量的值: %d\n", a )
a 变量的地址是: c00000a088
ip 变量储存的指针地址: c00000a088
*ip 变量的值: 20
a 变量的值: 20
package main
import "fmt"
func main() {
//指针数组
a := []int{10, 100, 200}
var i int //变量
var ptr [3]*int //指针变量
for i = 0; i < 3; i++ {
ptr[i] = &a[i]
}
for i = 0; i < 3; i++ {
fmt.Printf("a[%d] = %d\n", i, *ptr[i])
}
}
var a int
var ptr *int //指针变量
var pptr **int //指向指针的指针
a = 3000
ptr = &a //指针ptr地址
pptr = &ptr //指向指针ptr地址
fmt.Printf("变量 a = %d\n", a )
fmt.Printf("指针变量 *ptr = %d\n", *ptr )
fmt.Printf("指向指针的指针变量 **pptr = %d\n", **pptr)
原文:https://www.cnblogs.com/tanbinghao/p/14748645.html