Go基础学习(二)

数组【array】

数组定义【定义后长度不可变】

1
2
symbol := [...]string{USD: "$", EUR: "€", GBP: "£", RMB: "¥"}
fmt.Println(RMB, symbol[RMB])

指针数组

1
2
3
4
5
6
7
8
9
10
11
12
13
var array3 [5]*int
for i := range array3 {
array3[i] = new(int)
}
*array3[0] = 1
*array3[1] = 2
*array3[2] = 3
*array3[3] = 4
*array3[4] = 5
array4 := array3
for _, v := range array4 {
fmt.Println(*v)
}

切片【可变数组】

切片在底层维护一个可以动态扩展的数组,切片这一数据结构包含三个元素,指针、长度、容量【所以切片是引用类型】

1
2
3
4
5
6
7
8
source := make([]string, 3, 5) // 访问长度3个元素,底层数组拥有5个元素,不允许创建容量小于长度的切片
source = []string{"red", "green", "blue", "yellow", "black"}
slice := source[2:3:5] // 1个元素,容量为5-2=3
for _, v := range slice {
fmt.Println(v)
}
fmt.Println(len(slice))
fmt.Println(cap(slice))

切片、字符串互相转换

1
2
3
4
5
6
source := make([]string, 10)
source = []string{"123", "456", "789"}
a := strings.Join(source, ",") // 切片转字符串
m := strings.Split(a, ",")
fmt.Println(a)
fmt.Println(m)

中间插入元素

1
2
3
4
5
6
7
a := []int{1, 2, 3, 4, 5}
a = append(a, 0)
fmt.Println(a)
copy(a[3:], a[2:]) // 将[2:]的元素拷贝到[3:]中,最后一个元素丢弃
fmt.Println(a)
a[2] = 10 // 修改元素为新元素
fmt.Print(a)

原地删除

1
2
3
4
5
6
7
8
9
// 原地删除,公用底层数组
s := []string{"a", "b", " ", "d", "e", " ", "f"}
b := s[:0]
for _, x := range s {
if x != " " {
b = append(b, x) // append函数用于追加元素
}
}
fmt.Println(b)

slice作为参数

1
2
3
4
5
6
7
// more本质上就是slice类型
func Sum(a int, more ...int) int {
for _, v := range more {
a += v
}
return a
}

Map

基本操作

1
2
3
4
5
6
7
8
9
10
dict := make(map[string]int)
dict["abc"] = 123
dict["efg"] = 456
fmt.Println(dict["abc"])
_, exist := dict[ 大专栏  Go基础学习(二)4;abcd"]
fmt.Println(exist) // 判断是否存在,如果不赋值,exist就是false,赋值哪怕是零值,也为true
delete(dict, "abcd") // 即使key不存在也不会报错
for index, value := range dict { // map遍历是无序的
fmt.Println(index, value)
}

map类型也是引用类型,所以如果作为参数传到函数中修改,会改变变量本身

struct

struct不是引用类型,改变自身需要传指针
继承

1
2
3
4
5
6
7
8
type Point struct {
X, Y int
}

type Circle struct {
Point
Radius int
}

interface

interface是一组method的组合,可以通过interface来定义对象的一组行为。如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。
如果我们定义了一个interface的变量,那么这个变量里面可以存实现这个interface的任意类型的对象。
类型断言,判断一个interface是否是该类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import "fmt"

type Element interface{}
type List []Element
type People struct {
Name string
}

func main() {
list := make(List, 3)
list[0] = 1
list[1] = "hello"
list[2] = People{"test"}
for _, value := range list {
n, ok := value.(int)
if ok {
fmt.Println(n)
continue
}
s, ok := value.(string)
if ok {
fmt.Println(s[:3])
continue
}
p, ok := value.(People)
if ok {
fmt.Println(p.Name)
continue
}
}
}

使用switch来简化代码【element.(type)】不能在switch外的任何逻辑里面使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import "fmt"

type Element interface{}
type List []Element
type People struct {
Name string
}

func main() {
list := make(List, 4)
list[0] = 1
list[1] = "hello"
list[2] = People{"test"}
list[3] = 2.3
for _, value := range list {
switch t := value.(type) {
case int:
fmt.Println(t + 1)
case string:
fmt.Println(t[:3])
case People:
fmt.Println(t.Name)
default:
fmt.Println("类型异常")
}
}
}

interface也可以向struct一样内嵌

原文地址:https://www.cnblogs.com/lijianming180/p/12251564.html