推廣做網(wǎng)站怎么樣電子商務(wù)網(wǎng)站
Go-知識(shí)struct
- 1. struct 的定義
- 1.1 定義字段
- 1.2 定義方法
- 2. struct的復(fù)用
- 3. 方法受體
- 4. 字段標(biāo)簽
- 4.1 Tag是Struct的一部分
- 4.2 Tag 的約定
- 4.3 Tag 的獲取
githupio地址:https://a18792721831.github.io/
1. struct 的定義
Go 語(yǔ)言的struct與Java中的class類似,可以定義字段和方法,但是不能繼承。
1.1 定義字段
struct定義字段非常簡(jiǎn)單,只需要將字段寫在struct的結(jié)構(gòu)中,比如:
type tes struct {a intName string
}
需要注意的是,在Go里面,訪問權(quán)限是通過name的大小寫指定的,小寫表示包內(nèi)可見,如果是大寫則表示包外可見。
所以上面的struct如果用Java翻譯:
class tes {private int a;public String Name;
}
同樣的,如果創(chuàng)建的struct想讓包外可見,那么必須是大寫開頭。
type Tes struct{id intName string
}
1.2 定義方法
在Go里面一般不會(huì)區(qū)分函數(shù)和方法,或者更好理解的話,可以認(rèn)為方法是受限的函數(shù),限制了函數(shù)調(diào)用者,那么就是方法。
定義方法:
func (a tes) test() {fmt.Println(a.id)
}
同樣的,上述方法包內(nèi)可見。
func (a tes) Test() {fmt.Println(a.Name)
}
上述方法雖然包外可見,但是沒有意義,因?yàn)?code>tes是包內(nèi)可見,如果沒有對(duì)外提供函數(shù),那么是沒有意義的。
如果想保證安全,可以使用包內(nèi)可見的struct
配合包內(nèi)字段加包外方法,另外額外提供包外可見的struct
獲取函數(shù),實(shí)現(xiàn)類似于Java的可見性控制。
package testype person struct {id intname stringage int
}func (this *person) GetId() int {return this.id
}func (this *person) GetName() string {return this.name
}func (this *person) GetAge() int {return this.age
}func (this *person) SetId(id int) {this.id = id
}func (this *person) SetName(name string) {this.name = name
}func (this *person) SetAge(age int) {this.age = age
}func NewPerson() *person {return &person{}
}func NewPersonWithId(id int) *person {return &person{id: id}
}func NewPersonWithName(name string) *person {return &person{name: name}
}
因?yàn)镚o不支持函數(shù)重載,所以需要用不同的函數(shù)名字區(qū)分。
上述代碼實(shí)際上就是一個(gè)基本的JavaBean的實(shí)現(xiàn)。
但是實(shí)際使用上,基本上對(duì)外可見的字段都是直接用.
來訪問和賦值的。
在使用上,struct是否對(duì)外可見,則和編碼風(fēng)格相關(guān),業(yè)務(wù)系統(tǒng)一般不會(huì)考慮封閉性,基本上struct都是可見的;而第三方包等為了保證安全性,則會(huì)將部分struct設(shè)置為包內(nèi)可見,在結(jié)合interface
來保證擴(kuò)展性。
2. struct的復(fù)用
在其他編程語(yǔ)言中,使用繼承或組合實(shí)現(xiàn)代碼的復(fù)用。
而Go語(yǔ)言中沒有繼承,只能使用組合實(shí)現(xiàn)復(fù)用。
比較特別的是,在Go語(yǔ)言中,組合復(fù)用的struct可以認(rèn)為拷貝了被組合的struct的字段到需要的struct中。
type Man struct {personsex string
}func (this *Man) ToString() string {return fmt.Sprintf("id=%d, name=%s, age=%d, sex=%s\n", this.person.id, this.person.name, this.person.age, this.sex)
}func (this *Man) GetToString() string {return fmt.Sprintf("id=%d, name=%s, age=%d, sex=%s\n", this.id, this.name, this.age, this.sex)
}
在struct中組合其他struct,相當(dāng)于是創(chuàng)建了一個(gè)同名的隱式字段,在使用的時(shí)候,可以指明隱式字段,也可以不指明隱式字段。
想一想,在Java中,如果當(dāng)前class和父class中有同名的字段,那么在使用父類中的字段時(shí),需要使用super
指明使用的是父類中的字段。
同理的,當(dāng)struct中有一個(gè)id,那么在使用的時(shí)候,可以使用隱式字段指明:
type Man struct {id intpersonsex string
}func (this *Man) GetSuperId() int {return this.person.id
}func (this *Man) GetManId() int {return this.id
}
隱式字段如果顯示的定義了,那么就無法像使用自己的字段一樣使用內(nèi)嵌字段了:
type Woman struct {person personsex string
}func (this *Woman) ToString() string {return fmt.Sprintf("id=%d, name=%s, age=%d, sex=%s\n", this.person.id, this.person.name, this.person.age, this.sex)
}func (this *Woman) GetString() string {return fmt.Sprintf("id=%d, name=%s, age=%d, sex=%s\n", this.id)
}
如果還像使用自己的字段一樣使用內(nèi)嵌字段,就會(huì)找不到
3. 方法受體
方法本質(zhì)上還是函數(shù),只是限制了函數(shù)的調(diào)用者。
那么你有沒有好奇,為什么上面的例子中,方法的調(diào)用者都是指針類型而不是struct類型,這有什么區(qū)別?
type person struct {id intname stringage int
}func (this *person) SetIdPtr(id int) {this.id = id
}func (this person) SetId(id int) {this.id = id
}func (this *person) GetIdPtr() int {return this.id
}func (this person) GetId() int {return this.id
}func TestPerson(t *testing.T) {p := person{id: 1,name: "zhangsan",age: 10,}fmt.Printf("%+v\n", p)p.SetId(2)fmt.Printf("%+v\n", p)p.SetIdPtr(3)fmt.Printf("%+v\n", p)p.id = 4fmt.Printf("%+v\n", p.GetIdPtr())p.id = 5fmt.Printf("%+v\n", p.GetId())
}
沒錯(cuò),區(qū)別在于是否會(huì)影響原數(shù)據(jù)。
函數(shù)調(diào)用過程中,會(huì)將函數(shù)壓入調(diào)用棧,在入棧過程中,會(huì)對(duì)函數(shù)參數(shù)進(jìn)行拷貝。
在Java中,如果是基本類型參數(shù),那么拷貝值,如果是復(fù)雜類型參數(shù),那么拷貝指針。
在Go語(yǔ)言中,可以由程序員指定,如果方法調(diào)用者是指針,那么表示方法可以修改外部數(shù)據(jù),如果方法調(diào)用者是struct,那么不會(huì)修改外部數(shù)據(jù)。
如果是數(shù)據(jù)的讀取,那么不管是指針還是struct,都能讀取到數(shù)據(jù)。
在換一個(gè)角度看,方法的調(diào)用者,在方法調(diào)用的時(shí)候,也進(jìn)行了參數(shù)拷貝,所以可以認(rèn)為方法調(diào)用者就是一個(gè)特殊的參數(shù)。
type person struct {id intname stringage int
}func (this person) GetNameS() string {return this.name
}func GetName(this *person) string {return this.name
}func TestPerson(t *testing.T) {p := person{id: 1,name: "zhangsan",age: 10,}fmt.Println(p.GetNameS())fmt.Println(GetName(&p))
}
運(yùn)行都能獲取到結(jié)果
只是無法使用.
的方式觸發(fā)了。
4. 字段標(biāo)簽
在Go語(yǔ)言的struct的字段后面,可以使用標(biāo)簽。
type person struct {id int `tagKey:"tagValue1,tageValue2"`name string `tagKey:"tagValue1,tageValue2"`age int `tagKey:"tagValue1,tageValue2"`
}
4.1 Tag是Struct的一部分
Tag用于標(biāo)識(shí)字段的額外屬性,類似注釋。標(biāo)準(zhǔn)庫(kù)reflect
包中提供了操作Tag的方法。
// A StructField describes a single field in a struct.
type StructField struct {// Name is the field name.Name string// PkgPath is the package path that qualifies a lower case (unexported)// field name. It is empty for upper case (exported) field names.// See https://golang.org/ref/spec#Uniqueness_of_identifiersPkgPath stringType Type // field typeTag StructTag // field tag stringOffset uintptr // offset within struct, in bytesIndex []int // index sequence for Type.FieldByIndexAnonymous bool // is an embedded field
}
而StructTag
其實(shí)就是字符串:
4.2 Tag 的約定
Tag本質(zhì)上是個(gè)字符串,那么任何字符串都是合法的,但是在實(shí)際使用中,有一個(gè)約定:key:"value.."
格式,如果有多個(gè),中間用空格區(qū)分。
type person struct {id int `tagKey:"tagValue1,tageValue2" tagKey1:"tagValue1,tageValue2"`name string `tagKey:"tagValue1,tageValue2" tagKey1:"tagValue1,tageValue2"`age int `tagKey:"tagValue1,tageValue2" tagKey1:"tagValue1,tageValue2"`
}
key: 必須是非空字符串,字符串不能包含控制字符、空格、引號(hào)、冒號(hào)。
value: 以雙引號(hào)標(biāo)記的字符串。
key和value之間使用冒號(hào)分割,冒號(hào)前后不能有空格。
多個(gè)key-value之間用空格分割。
key一般用于表示用途,value一般表示控制指令。
比如:
json:"name,omitempty"
表示json
轉(zhuǎn)換的時(shí)候,使用name
作為名字,如果字段值為空,那么json
轉(zhuǎn)換該字段的時(shí)候忽略。
4.3 Tag 的獲取
在reflect
的StructField
提供了Get
和Lookup
方法:
比如獲取上面person
的Tag
func TestPerson(t *testing.T) {p := person{id: 1,name: "zhangsan",age: 10,}st := reflect.TypeOf(p)stf, ok := st.FieldByName("id")if !ok {fmt.Println("not found")return}nameTag := stf.Tagfmt.Printf("tagKey=%s\n", nameTag.Get("tagKey"))tagValue, ok := nameTag.Lookup("tagKey1")if !ok {fmt.Println("not found")return}fmt.Printf("tagKey1=%s\n", tagValue)
}
在Java中有一個(gè)非常強(qiáng)大,也經(jīng)常使用的插件lombok
,通過在class的字段上添加注解,進(jìn)而實(shí)現(xiàn)一些控制方法。
區(qū)別在于,lombok
是在編譯時(shí),通過操作字節(jié)碼,實(shí)現(xiàn)方法的寫入,而Tag是在運(yùn)行時(shí),通過反射賦值。
所以Tag只能操作已有的字段和函數(shù),不能動(dòng)態(tài)的增加或者減少字段和函數(shù)。
除了使用第三方庫(kù),借助上述語(yǔ)法,自己也可以定義需要的操作比如判空。