Go 開發常用操作技巧 -- 結構體
結構體是由一系列相同或不相同類型數據組成的數據集合。 結構體的成員(字段)的特性:
-
字段名必須唯一
-
字段有自己的類型和值
-
字段的類型也可以是結構體,甚至是字段所在結構體的類型
結構體定義格式如下:
type 類型名 struct{
字段1 類型1
字段2 類型2
//...
}
結構體初始化
type Mystruct struct {
Name string
Address string
}
//方法一
var person Mystruct
person.Name = "test"
person.Address = "beijing"
//方法二
person1 := Mystruct{"test","beijing"}
//方法三
//(*person2).Name 等同於 person2.Name
var person2 = new(Mystruct)
person2.Name = "test"
person2.Address = "beijing"
//方法四
//(*Mystruct).Name 等同於 person3.Name
var person3 = &Mystruct{}
(*person3).Name = "test"
(*person3).Address = "beijing"
結構體排序
可以通過 sort.Slice() 函數進行結構體的排序
示例:
package main
import (
"fmt"
"sort"
)
type Mystruct struct {
Name string
Address string
}
func main() {
persons := []Mystruct{
{"test2", "aa"},
{"test1", "bb"},
{"test3", "cc"},
}
fmt.Println(persons)
sort.Slice(persons, func(i, j int) bool {
return persons[i].Name < persons[j].Name || persons[i].Address < persons[j].Address
})
fmt.Println(persons)
}
運行結果:
[{test2 aa} {test1 bb} {test3 cc}]
[{test1 bb} {test2 aa} {test3 cc}]
結構體繼承
Go 語言中沒有 extends 關鍵字,不能像其他語言那樣去實現類的繼承。在 Go 中,使用的是 interface 實現的功能組合,以組合的形式來實現繼承。
如下我們創建一個父類結構體 Father,再用子類 Child 繼承父類,實現在 Child 中調用父類的方法,示例:
package main
import (
"fmt"
)
type Father struct {
Name string
}
func (f Father) Say(){
fmt.Println("father...")
}
type Child struct {
Father
}
func main() {
child := Child{}
child.Father.Say() // father...
//直接使用下面的寫法也可以,Go語言做了相關的優化,直接調用方法也可以找到父類中的方法
child.Say() // father...
}
改寫父類的方法
package main
import (
"fmt"
)
type Father struct {
Name string
}
func (f Father) Say(){
fmt.Println("father...")
}
type Child struct {
Father
}
func (c Child)Say(){
fmt.Println("child start...")
c.Father.Say()
fmt.Println("child end...")
}
func main() {
child := Child{}
child.Father.Say()
//被 child 改寫後,下面調用的就是自身的方法
child.Say()
}
運行結果:
father...
child start...
father...
child end...
匿名結構體
顧名思義,匿名結構體就是沒有名字的結構體,示例如下:
package main
import "fmt"
func main() {
book := struct { // 無需事先聲明
title string
id int
}{
title: "Go語言", // 定義完成之後,即可進行初始化
id: 123456,
}
fmt.Println("title ", book.title)
fmt.Println("id", book.id)
}
// 打印結果
// title Go語言
// id 123456
結構體字段標籤
結構體字段標籤(tag)是結構體額外的信息,用於對字段進行說明。在 json 序列化及對象關係映射時,會用到此標籤。標籤信息都是靜態的,無需實例化結構體,可以通過反射來獲得。
標籤在結構體後面,由一個或多個鍵值組成,鍵值間使用空格分隔:
key1:"value1" key2:"value2"
使用反射獲取結構體標籤信息:
package main
import (
"fmt"
"reflect"
)
type Test struct {
Name string `json:"name"`
}
func main() {
var test = Test{}
//反射獲取標籤信息
typeTest := reflect.TypeOf(test)
testFieldName, ok := typeTest.FieldByName("Name")
if ok {
fmt.Println(testFieldName.Tag.Get("json"))
}
}
// 打印結果
// name
多態
-
繼承主要在於: 解決代碼的複用性和可維護性;
-
接口主要在於: 接口設計好各種規範方法,讓其它自定義類型實現這些方法;
-
接口比繼承更加靈活, 繼承是滿足
is-a
的關係, 接口是滿足like-a
的關係;
Go 中的多態是通過接口實現的:
package main
import "fmt"
type People interface {
Say()
}
type Student struct {}
type Teacher struct {}
func (p *Student) Say(){
fmt.Println("Student...")
}
func (c *Teacher) Say(){
fmt.Println("Teacher...")
}
func hello(p People) {
p.Say()
}
func main(){
var student = new(Student)
var teacher = new(Teacher)
hello(student)
hello(teacher)
}
打印結果:
Student...
Teacher...
微信公衆號
gophpython
我的微信
wucs_dd
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/a_yVeUXg-h6MxaAiARB9Ig