摘要: Go的接口使用interface关键词定义。 接口定义: // 接口 type Movable interface { move(speed int) int } 接口实现: 第一个实现,speed * 2 type Cat struct { } // 函数原型一样,实现了Movable接口 fun 阅读全文
posted @ 2021-07-24 21:20 HiIT青年 阅读(46) 评论(0) 推荐(0) 编辑
摘要: Go使用json包的Marshal和Unmarshal进行json的序列化和反序列化。 json 序列化: // 定义结构体 type Cat struct { Name string Age int8 } // 序列化 c := Cat{"Tom", 2} bytes, err := json.M 阅读全文
posted @ 2021-07-24 20:57 HiIT青年 阅读(379) 评论(0) 推荐(0) 编辑
摘要: 基于结构体实现Go的面向对象编程 // person结构体-person类 type person struct { name string age int8 } // person成员方法 func (p person) sayHi() { fmt.Printf("%s say Hi!\n", p 阅读全文
posted @ 2021-07-24 20:44 HiIT青年 阅读(40) 评论(0) 推荐(0) 编辑
摘要: Go语言的函数和方法不是同一个概念。 普通函数: // 普通函数 func demo0(a, b int) int { return a + b } // 调用 ret := demo0(1, 2) fmt.Println(ret) 匿名函数: // 匿名函数, 直接调用 func demo1() 阅读全文
posted @ 2021-07-24 20:31 HiIT青年 阅读(80) 评论(0) 推荐(0) 编辑
摘要: Go语言的结构体跟C语言的结构体有点类似,不过定义方法有点区别。 结构体定义: type person struct { name string age int8 } 结构体初始化: p0 := person{"Hi", 18} p1 := person{name: "Go", age: 19} p 阅读全文
posted @ 2021-07-24 20:18 HiIT青年 阅读(41) 评论(0) 推荐(0) 编辑