Home > Article > Backend Development > Structure of the basics of Go language (winter)
Speaking of inheritance, anyone who has studied Java must be familiar with Python. , but there is no such thing in Go.
What should we do???, we still have to use a structure to achieve it.
Pretend that we are all boys and like cars, so let’s take cars as an example.
//车 type Car struct { Brand string //车品牌 CarNum string //车牌号 Tyre int //轮胎个数 } //给车绑定一个方法,说明车的基本信息 func (this *Car) carInfo() { fmt.Printf("品牌:%s,车牌号:%s,轮胎个数:%d\n", this.Brand, this.CarNum, this.Tyre) }
//宝马车 type BMWCar struct { //*Car和Car基本没有区别,一个存的是整个结构体,一个存的是结构体地址,用法大同小异 *Car //这就表示继承了Car这个结构体 }
//比亚迪车 type BYDCar struct { *Car }
may be seen You will feel familiar with this. Isn't this the nested structure we used in the previous lesson? ? ?
Does this have anything to do with inheritance?
其实在Go中,结构体既可以用来存储数据,也可以用来模仿对象的各种操作。
func main() { //一个宝马对象 var bmw1 = BMWCar{&Car{ Brand: "宝马x8", CarNum: "京666", Tyre: 4, }} //一个比亚迪对象 var byd1 = BYDCar{&Car{ Brand: "比亚迪L3", CarNum: "京111", Tyre: 4, }} //因为 BMWCar 和 BYDCar 都继承了Car,所以都有carInfo这个方法 bmw1.carInfo() byd1.carInfo() }
这就是一个最简单的,面向对象,跟其他语言一样,继承会将所有的属性和方法都继承过来。
到此为止呢,结构体基本可以告一段落了,基本算是入门了,当然,并没有结束,但是我想大家都累了,换个方向继续玩。
这个东西叫做序列化,什么意思呢,就是像咱们的切片了,map了,结构体了等,这些都是Go的类型。
如果要和其他语言交流,人家可没有这些玩意唉,那怎么办呢???
众多大佬就形成了一个规范,json
数据格式,json
数据必须是字符串类型。
最外面是'
号,键/值对组合中的键名写在前面并用双引号""
包裹。
就像这样。
'{"Gender":"男","Name":"张三"}' //'说明这个是字符串,一般打印时不显示
序列化我们用到的是json
模块的Marshal
方法。
单独的切片序列化用的很少,但是仍然还是要知道。
示例代码
package main import ( "encoding/json" "fmt" ) type Student struct { Gender string Name string } func main() { var StudentList = []string{"张三", "李四"} fmt.Printf("StudentList类型:%T\n", StudentList) //[]string,这是列表类型 serializeByte, err := json.Marshal(StudentList) if err != nil { fmt.Println("序列化失败") return } var serializeStr = string(serializeByte) fmt.Printf("serializeStr类型:%T\n", serializeStr) //string,这是字符串类型 fmt.Printf("serializeStr值:%v\n", serializeStr) //["张三","李四"] }
第16行代码将切片序列化,但是返回的是[]byte
类型,第21行代码将[]byte
类型转成字符串。
执行结果
字典序列化,就比较有味道了,序列化的是一个标准的json
数据格式。
示例代码
package main import ( "encoding/json" "fmt" ) type Student struct { Gender string Name string } func main() { var StudentInfo = map[string]string{ "Name":"张三", "Age":"18", "Gender":"男", } fmt.Printf("StudentInfo类型:%T\n",StudentInfo) serializeByte, err := json.Marshal(StudentInfo) if err != nil { fmt.Println("序列化失败") } var serializeStr = string(serializeByte) fmt.Printf("serializeStr类型:%T\n", serializeStr) //string,这是字符串类型 fmt.Printf("serializeStr值:%v\n", serializeStr) //{"Age":"18","Gender":"男","Name":"张三"} }
执行结果
这个就有点像标准的json
格式了。
type Student struct { Name string Gender string Age int }
func main() { var s1 = Student{ Name: "张三", Gender: "男", Age: 18, } fmt.Printf("StudentInfo类型:%T\n", s1) serializeByte, err := json.Marshal(s1) if err != nil { fmt.Println("序列化失败") } var serializeStr = string(serializeByte) fmt.Printf("serializeStr类型:%T\n", serializeStr) //string,这是字符串类型 fmt.Printf("serializeStr值:%v\n", serializeStr) }
执行结果
一般情况下,这种方式数据格式是用的比较多的。
当然, 还可以切片嵌套map,方法和此方法一样,不做例子了。
示例代码
package main import ( "encoding/json" "fmt" ) type Student struct { Name string Gender string Age int } func main() { var s1 = Student{ Name: "张三", Gender: "男", Age: 18, } var s2 = Student{ Name: "李四", Gender: "女", Age: 16, } //一个存放 Student 的列表 var studentList = []Student{s1, s2} fmt.Printf("StudentInfo类型:%T\n", studentList) serializeByte, err := json.Marshal(studentList) //main.Student if err != nil { fmt.Println("序列化失败") } var serializeStr = string(serializeByte) fmt.Printf("serializeStr类型:%T\n", serializeStr) //string,这是字符串类型 fmt.Printf("serializeStr值:%v\n", serializeStr) }
执行结果
Tag
可以理解为结构体的说明,由一对反引号包裹起来。
但是一般情况下,Tag在序列化是用的比较多。
type Student struct { Name string `json:"name"` Gender string `json:"gender"` Age int `json:"age"` }
每个字段后面跟的,就是Tag,一定不要把格式搞错啦。
func main() { var s1 = Student{ Name: "张三", Gender: "男", Age: 18, } fmt.Printf("StudentInfo类型:%T\n", s1) serializeByte, err := json.Marshal(s1) //main.Student if err != nil { fmt.Println("序列化失败") } var serializeStr = string(serializeByte) fmt.Printf("serializeStr类型:%T\n", serializeStr) //string,这是字符串类型 fmt.Printf("serializeStr值:%v\n", serializeStr) }
执行结果
可以发现key成小写的了,这就说明一个问题。
During serialization, if the structure has json
This Tag will be serialized based on json
Tag. If there is no json
Tag, the structure field shall prevail.
Above we have learned the basic structure of Go Structure inheritance,Serialization,Structure tag. After learning Go's structure, you may also know how to imitate object-oriented in Go.
The above is the detailed content of Structure of the basics of Go language (winter). For more information, please follow other related articles on the PHP Chinese website!