0
点赞
收藏
分享

微信扫一扫

Go-知识struct

Go-知识struct

githupio地址:https://a18792721831.github.io/

1. struct 的定义

Go 语言的struct与Java中的class类似,可以定义字段和方法,但是不能继承。

1.1 定义字段

struct定义字段非常简单,只需要将字段写在struct的结构中,比如:

type tes struct {
	a int
	Name string
}

需要注意的是,在Go里面,访问权限是通过name的大小写指定的,小写表示包内可见,如果是大写则表示包外可见。
所以上面的struct如果用Java翻译:

class tes {
    private int a;
    public String Name;
} 

同样的,如果创建的struct想让包外可见,那么必须是大写开头。

type Tes struct{
    id int
    Name string
}

1.2 定义方法

在Go里面一般不会区分函数和方法,或者更好理解的话,可以认为方法是受限的函数,限制了函数调用者,那么就是方法。
定义方法:

func (a tes) test() {
    fmt.Println(a.id)
}

同样的,上述方法包内可见。

func (a tes) Test() {
    fmt.Println(a.Name)
}

上述方法虽然包外可见,但是没有意义,因为tes是包内可见,如果没有对外提供函数,那么是没有意义的。
如果想保证安全,可以使用包内可见的struct配合包内字段加包外方法,另外额外提供包外可见的struct获取函数,实现类似于Java的可见性控制。

package tes

type person struct {
	id   int
	name string
	age  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}
}

因为Go不支持函数重载,所以需要用不同的函数名字区分。
上述代码实际上就是一个基本的JavaBean的实现。
但是实际使用上,基本上对外可见的字段都是直接用.来访问和赋值的。
在使用上,struct是否对外可见,则和编码风格相关,业务系统一般不会考虑封闭性,基本上struct都是可见的;而第三方包等为了保证安全性,则会将部分struct设置为包内可见,在结合interface来保证扩展性。

2. struct的复用

在其他编程语言中,使用继承或组合实现代码的复用。
而Go语言中没有继承,只能使用组合实现复用。
比较特别的是,在Go语言中,组合复用的struct可以认为拷贝了被组合的struct的字段到需要的struct中。

type Man struct {
	person
	sex 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,相当于是创建了一个同名的隐式字段,在使用的时候,可以指明隐式字段,也可以不指明隐式字段。
想一想,在Java中,如果当前class和父class中有同名的字段,那么在使用父类中的字段时,需要使用super指明使用的是父类中的字段。
同理的,当struct中有一个id,那么在使用的时候,可以使用隐式字段指明:

type Man struct {
	id int
	person
	sex string
}

func (this *Man) GetSuperId() int {
	return this.person.id
}

func (this *Man) GetManId() int {
	return this.id
}

隐式字段如果显示的定义了,那么就无法像使用自己的字段一样使用内嵌字段了:

type Woman struct {
	person person
	sex    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)
}

如果还像使用自己的字段一样使用内嵌字段,就会找不到
在这里插入图片描述

3. 方法受体

方法本质上还是函数,只是限制了函数的调用者。
那么你有没有好奇,为什么上面的例子中,方法的调用者都是指针类型而不是struct类型,这有什么区别?

type person struct {
	id   int
	name string
	age  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 = 4
	fmt.Printf("%+v\n", p.GetIdPtr())
	p.id = 5
	fmt.Printf("%+v\n", p.GetId())
}

在这里插入图片描述

没错,区别在于是否会影响原数据。
函数调用过程中,会将函数压入调用栈,在入栈过程中,会对函数参数进行拷贝。
在Java中,如果是基本类型参数,那么拷贝值,如果是复杂类型参数,那么拷贝指针。
在Go语言中,可以由程序员指定,如果方法调用者是指针,那么表示方法可以修改外部数据,如果方法调用者是struct,那么不会修改外部数据。
如果是数据的读取,那么不管是指针还是struct,都能读取到数据。
在换一个角度看,方法的调用者,在方法调用的时候,也进行了参数拷贝,所以可以认为方法调用者就是一个特殊的参数。

type person struct {
	id   int
	name string
	age  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))
}

运行都能获取到结果
在这里插入图片描述

只是无法使用.的方式触发了。

4. 字段标签

在Go语言的struct的字段后面,可以使用标签。

type person struct {
	id   int    `tagKey:"tagValue1,tageValue2"`
	name string `tagKey:"tagValue1,tageValue2"`
	age  int    `tagKey:"tagValue1,tageValue2"`
}

4.1 Tag是Struct的一部分

Tag用于标识字段的额外属性,类似注释。标准库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_identifiers
	PkgPath string

	Type      Type      // field type
	Tag       StructTag // field tag string
	Offset    uintptr   // offset within struct, in bytes
	Index     []int     // index sequence for Type.FieldByIndex
	Anonymous bool      // is an embedded field
}

StructTag其实就是字符串:
在这里插入图片描述

4.2 Tag 的约定

Tag本质上是个字符串,那么任何字符串都是合法的,但是在实际使用中,有一个约定:key:"value.."格式,如果有多个,中间用空格区分。

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一般用于表示用途,value一般表示控制指令。
比如:
json:"name,omitempty"
表示json转换的时候,使用name作为名字,如果字段值为空,那么json转换该字段的时候忽略。

4.3 Tag 的获取

reflectStructField提供了GetLookup方法:
在这里插入图片描述

比如获取上面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.Tag
	fmt.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中有一个非常强大,也经常使用的插件lombok,通过在class的字段上添加注解,进而实现一些控制方法。
区别在于,lombok是在编译时,通过操作字节码,实现方法的写入,而Tag是在运行时,通过反射赋值。
所以Tag只能操作已有的字段和函数,不能动态的增加或者减少字段和函数。
除了使用第三方库,借助上述语法,自己也可以定义需要的操作比如判空。

举报

相关推荐

go-基础-defer

go-猜谜游戏

go-计算器

go-基础-错误终止-panic

go-基础-任务池

go struct 工厂

go-基础-格式化

0 条评论