基本语法
array数组
// 第一种
var x [10]int
x[1]=1
x[9]=10
fmt.Printf("%v",x)
// 第二种
x:=[10]int{1,10}
fmt.Printf("%v\n",x)
fmt.Printf("%v",len(x))
slice动态数组
申明
var x []int
使用:=申明
s:=make([]int,3,5)
fmt.Printf("%v", len(s))
3
3为数组长度,5为建议容量 实际使用中如果超过这个值 底层会自动扩展的
s :=make([]int,3,5)
s = append(s,1,2,3,4)
fmt.Printf("%v", s)
[0 0 0 1 2 3 4]
通过一个数组或已经存在的slice中再次声明
x :=[10]int{1,2,3,4,5,6,7,8,9,10}
y :=x[1:3]
fmt.Printf("%v",y)
[2 3]
map
map必须要make
申明
1.
var student map[string]int
student = make(map[string]int)
student["zhangsan"]=1
fmt.Printf("%v", student)
map[zhangsan:1]
student := make(map[string]int)
student["zhangsan"]=1
fmt.Printf("%v", student)
map[zhangsan:1]
range
用于便捷地遍历容器中的元素
key和value都遍历
student := make(map[string]int)
student["zhangsan"]=1
student["lisi"]=2
student["wangwu"]=3
for i,v := range student{
fmt.Println(i,v)
}
zhangsan 1
lisi 2
wangwu 3
只遍历value
x := [3]int{1,2,3}
for _,v := range x {
fmt.Println(v)
}
1
2
3
遍历字符串
x := "zhangsan"
for _,v := range x{
fmt.Printf("%c\n", v)
}
z
h
a
n
g
s
a
n
函数
简单函数
func main() {
a :=1
b :=2
a,b = swap(a,b)
fmt.Printf("%d %d", a,b)
}
func swap(a int, b int) (int,int) {
return b,a
}
2 1
func getSum(a []int) (int){
sum := 0
for _,v := range a {
sum+=v
}
return sum
}
func main() {
a := []int{1,2,3}
s := getSum(a)
fmt.Println(s)
}
6
匿名函数
func main() {
f := func (x,y int) int {
return x+y
}
fmt.Println(f(1,2))
}
3
defer
func main() {
for i :=1 ; i<5; i++ {
defer fmt.Println(i)
}
fmt.Println("before defer")
}
before defer
4
3
2
1
defer 后面跟着语句块是函数执行结束之前再执行,一般可以用在资源的关闭上。例如:
file.Open("file")
defer file.Close()
defer和匿名函数一起用
func main() {
defer func () {
fmt.Println("after defer")
}()
fmt.Println("before defer")
}
before defer
after defer
'()'匿名函数后面的()表示执行这个函数的意思等价于下面
func main() {
f := func () {
fmt.Println("after defer")
}
defer f()
fmt.Println("before defer")
}
对象
struct
type Person struct{
name string
age int
}
type Student struct{
Person
speciality string
}
func main() {
student := Student{Person{"zhangsan",18},"math"}
// student := Student{speciality:"math",Person:Person{"zhangsan",18}}
fmt.Printf("%v\n", student)
fmt.Printf("%v\n", student.speciality)
fmt.Printf("%v\n", student.age)
fmt.Printf("%v", student.Person.name)
}
{{zhangsan 18} math}
math
18
zhangsan
类的初始化
// 指针变量
point := new(Point)
point := &Point{}
point := &Point{x:100,y:100}
// 实例
point := Point{}
类的方法
type Point struct{
px float32
py float32
}
func (point *Point) setXY(px,py float32) {
point.px = px
point.py = py
}
func (point *Point) getXY() (float32,float32) {
return point.px,point.py
}
func main() {
point := new(Point)
point.setXY(1.2,3.4)
px,py := point.getXY()
fmt.Print(px,py)
}
1.2 3.4
接口
在go语言中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口。
把实例赋值给接口
type Animal interface{
Fly()
Run()
}
type Bird struct{
}
func (bird Bird) Fly() {
fmt.Println("Bird is flying!!!")
}
func (bird Bird) Run() {
fmt.Println("Bird is running!!!")
}
func main() {
var animal Animal
bird := new(Bird)
animal = bird
animal.Fly()
animal.Run()
}
Bird is flying!!!
Bird is running!!!
把接口赋值给接口只能把多的赋值给少的
type Animal interface{
Fly()
Run()
}
type Animal2 interface{
Fly()
}
type Bird struct{
}
func (bird Bird) Fly() {
fmt.Println("Bird is flying!!!")
}
func (bird Bird) Run() {
fmt.Println("Bird is running!!!")
}
func main() {
var animal Animal
var animal2 Animal2
bird := new(Bird)
animal = bird
animal2 = animal //划重点
animal2.Fly()
}
类型查询
func main() {
var v1 interface{}
v1 = 6.78
if v,ok :=v1.(float64); ok {
fmt.Println(v)
}
var v2 interface{}
v2 = 12
if v,ok := v2.(float32); ok{
fmt.Println(v)
}else {
fmt.Println("no float32")
}
}
6.78
no float32
func main() {
var v1 interface{}
v1 = 6.78
switch v1.(type) {
case int:
fmt.Println("this is int")
case float32:
fmt.Println("this is float32")
case float64:
fmt.Println("this is float64")
}
}
this is float64