docs: json 与 结构体互转、结构体嵌套、接口、空接口、类型断言

This commit is contained in:
Shikong 2021-10-04 02:45:59 +08:00
parent 03faf47f2d
commit 75fa0bc674
4 changed files with 171 additions and 0 deletions

84
base/interface/main.go Normal file
View File

@ -0,0 +1,84 @@
package main
import "fmt"
// 接口的定义
// type 接口名 interface {
// 方法名1(参数1, 参数2...)(返回值1, 返回值2...)
// ...
// }
// 一个结构体 如果实现了 接口中规定的所有方法
// 那么这个结构体 就 实现了这个接口, 也就是 java中 接口的 实现类
// 多个类型可以实现一个接口
// 一个类型可以实现多个接口
// 接口 可以嵌套 接口
// Empty 空接口
//type Empty interface { ... }
// 空接口 没有必要命名 可定义成以下形式
//interface{}
// 作为函数参数使用时 可传入任意类型变量
// Speaker 定义一个能叫的 类型 接口
type Speaker interface {
// Cell
// 接口内部 声明 方法签名
// 只要实现了 Cell 方法 都是 Speaker 类型
Cell()
}
// Animal 接口 嵌套
type Animal interface {
Speaker
}
type Cat struct {
}
// Cell 实现的 方法签名 必须 与接口 规定的一致
func (c *Cat) Cell() {
fmt.Printf("猫:叫\n")
}
type Dog struct {
}
func (d *Dog) Cell() {
fmt.Printf("狗:叫\n")
}
// Call
// 叫 谁 谁回应
func Call(speaker Animal) {
speaker.Cell()
}
// 空接口作为参数
func print(i interface{}) {
fmt.Printf("%T %#v", i, i)
}
func main() {
c1 := new(Cat)
d1 := new(Dog)
Call(c1)
Call(d1)
fmt.Println("=========================================================")
// 定义一个 Speaker 接口类型的变量
var speaker Speaker
speaker = c1
Call(speaker)
speaker = d1
Call(speaker)
fmt.Println("=========================================================")
print(c1)
print(d1)
}

40
base/struct/json/main.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"encoding/json"
"fmt"
)
// 结构体 与 JSON 互转
type Person struct {
// 字段可见性 同标识符一样 与 首字母 大小写 有关
// 小写开头 的 字段 不会被 json.Marshal 解析
// 使用 tag 标签 标识需要转换的字段名
Name string `json:"name" db:"name" yaml:"name"`
Age int `json:"age"`
}
func main() {
p1 := &Person{
Name: "张三",
Age: 20,
}
fmt.Printf("p1: %#v\n", p1)
fmt.Println("=========================================================")
// 序列化 结构体 => JSON
j1, _ := json.Marshal(p1)
fmt.Println(string(j1))
fmt.Println("=========================================================")
// 反序列化 JSON => 结构体
p2 := new(Person)
j2 := "{\"name\":\"李四\",\"age\":18}"
// 需传入 结构体 指针
_ = json.Unmarshal([]byte(j2), p2)
fmt.Printf("p2: %#v\n", p2)
}

View File

@ -51,6 +51,7 @@ func main() {
City: "广州", City: "广州",
}, },
} }
fmt.Printf("%#v\n", p2) fmt.Printf("%#v\n", p2)
fmt.Printf("%v %v\n", p2.Address.Province, p2.Address.City) fmt.Printf("%v %v\n", p2.Address.Province, p2.Address.City)
// 匿名结构体 先在自己的 结构体中 寻找 字段 若 找不到 则 从 匿名嵌套结构体中 查找 // 匿名结构体 先在自己的 结构体中 寻找 字段 若 找不到 则 从 匿名嵌套结构体中 查找

46
base/type_assert/main.go Normal file
View File

@ -0,0 +1,46 @@
package main
import "fmt"
// 类型断言
func assign(i interface{}) {
fmt.Printf("%T\n", i)
// 类型断言
str, ok := i.(string)
if !ok {
fmt.Println("传入的参数不是一个字符串")
} else {
fmt.Println("传入的参数为字符串", str)
}
}
func assign2(i interface{}) {
fmt.Printf("%T\n", i)
// 类型断言
switch i := i.(type) {
case string:
fmt.Printf("传入的参数为 %T 字符串: %#v\n", i, i)
case int:
fmt.Printf("传入的参数为 %T 整数: %#v\n", i, i)
default:
fmt.Printf("传入的参数为 %T 类型: %#v\n", i, i)
}
}
func main() {
assign(100)
fmt.Println("=========================================================")
assign2(100)
fmt.Println("=========================================================")
assign2(struct {
Name string
Age int
}{"张三", 20})
}