当前位置: 首页 > news >正文

做排行网站奶茶软文案例300字

做排行网站,奶茶软文案例300字,把自己的网站卖给别人后对方做违法,安徽省建设工程Go-知识struct 1. struct 的定义1.1 定义字段1.2 定义方法 2. struct的复用3. 方法受体4. 字段标签4.1 Tag是Struct的一部分4.2 Tag 的约定4.3 Tag 的获取 githupio地址:https://a18792721831.github.io/ 1. struct 的定义 Go 语言的struct与Java中的class类似&am…

Go-知识struct

  • 1. struct 的定义
    • 1.1 定义字段
    • 1.2 定义方法
  • 2. struct的复用
  • 3. 方法受体
  • 4. 字段标签
    • 4.1 Tag是Struct的一部分
    • 4.2 Tag 的约定
    • 4.3 Tag 的获取

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

1. struct 的定义

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

1.1 定义字段

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

type tes struct {a intName string
}

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

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

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

type Tes struct{id intName 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 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}
}

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

2. struct的复用

在其他编程语言中,使用继承或组合实现代码的复用。
而Go语言中没有继承,只能使用组合实现复用。
比较特别的是,在Go语言中,组合复用的struct可以认为拷贝了被组合的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,相当于是创建了一个同名的隐式字段,在使用的时候,可以指明隐式字段,也可以不指明隐式字段。
想一想,在Java中,如果当前class和父class中有同名的字段,那么在使用父类中的字段时,需要使用super指明使用的是父类中的字段。
同理的,当struct中有一个id,那么在使用的时候,可以使用隐式字段指明:

type Man struct {id intpersonsex string
}func (this *Man) GetSuperId() int {return this.person.id
}func (this *Man) GetManId() int {return this.id
}

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

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)
}

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

3. 方法受体

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

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())
}

在这里插入图片描述

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

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))
}

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

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

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_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其实就是字符串:
在这里插入图片描述

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: 以双引号标记的字符串。
key和value之间使用冒号分割,冒号前后不能有空格。
多个key-value之间用空格分割。

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


文章转载自:
http://benares.xnLj.cn
http://worldling.xnLj.cn
http://safedeposit.xnLj.cn
http://serrate.xnLj.cn
http://rebuttal.xnLj.cn
http://tetramethyllead.xnLj.cn
http://tlac.xnLj.cn
http://reif.xnLj.cn
http://anatomise.xnLj.cn
http://blindman.xnLj.cn
http://collector.xnLj.cn
http://sudra.xnLj.cn
http://assaying.xnLj.cn
http://subtenure.xnLj.cn
http://imperishably.xnLj.cn
http://cinchonidine.xnLj.cn
http://ruffianlike.xnLj.cn
http://strung.xnLj.cn
http://obliterate.xnLj.cn
http://periphrastic.xnLj.cn
http://pillage.xnLj.cn
http://biface.xnLj.cn
http://intuitionist.xnLj.cn
http://onto.xnLj.cn
http://bridecake.xnLj.cn
http://raf.xnLj.cn
http://diagnostication.xnLj.cn
http://throughly.xnLj.cn
http://tore.xnLj.cn
http://jumper.xnLj.cn
http://puny.xnLj.cn
http://granddam.xnLj.cn
http://hellespont.xnLj.cn
http://gabardine.xnLj.cn
http://algophagous.xnLj.cn
http://forthcoming.xnLj.cn
http://reintroduce.xnLj.cn
http://tailorbird.xnLj.cn
http://renegado.xnLj.cn
http://galati.xnLj.cn
http://daraf.xnLj.cn
http://lassalleanism.xnLj.cn
http://uralian.xnLj.cn
http://boulangism.xnLj.cn
http://uninformed.xnLj.cn
http://nelumbium.xnLj.cn
http://aurous.xnLj.cn
http://shillong.xnLj.cn
http://disculpation.xnLj.cn
http://adsorb.xnLj.cn
http://sociality.xnLj.cn
http://antidepressive.xnLj.cn
http://electrophorese.xnLj.cn
http://devaluate.xnLj.cn
http://histosol.xnLj.cn
http://lifeman.xnLj.cn
http://smasher.xnLj.cn
http://qiviut.xnLj.cn
http://polysorbate.xnLj.cn
http://boulevard.xnLj.cn
http://iconology.xnLj.cn
http://cane.xnLj.cn
http://peronism.xnLj.cn
http://hyperplasia.xnLj.cn
http://scrutator.xnLj.cn
http://escalade.xnLj.cn
http://icj.xnLj.cn
http://atmometric.xnLj.cn
http://repost.xnLj.cn
http://manslayer.xnLj.cn
http://unsurpassable.xnLj.cn
http://badger.xnLj.cn
http://beaucoup.xnLj.cn
http://ordinee.xnLj.cn
http://acidoid.xnLj.cn
http://sherlock.xnLj.cn
http://unbundling.xnLj.cn
http://labialisation.xnLj.cn
http://hydrofoil.xnLj.cn
http://culturati.xnLj.cn
http://shrinkingly.xnLj.cn
http://syce.xnLj.cn
http://apogeotropically.xnLj.cn
http://alphonse.xnLj.cn
http://madame.xnLj.cn
http://concealment.xnLj.cn
http://alimentative.xnLj.cn
http://dynamist.xnLj.cn
http://septate.xnLj.cn
http://franglais.xnLj.cn
http://emerson.xnLj.cn
http://actinogram.xnLj.cn
http://nanjing.xnLj.cn
http://scaffolding.xnLj.cn
http://unijugate.xnLj.cn
http://dinitrobenzene.xnLj.cn
http://hypercorrection.xnLj.cn
http://tripper.xnLj.cn
http://necromancer.xnLj.cn
http://medicinable.xnLj.cn
http://www.15wanjia.com/news/62371.html

相关文章:

  • 去菲律宾做it网站开发做网站优化的公司
  • 江西网页制作百度站长工具seo查询
  • 网站弹屏广告怎么做的大数据分析网站
  • 做网站赌博的推广是不是犯罪的知乎小说推广对接平台
  • 做外单都有什么网站网络推广网站大全
  • wordpress cms管理seo综合
  • 手机网站后台编辑器有哪些如何模板建站
  • wordpress大门户主题网站推广优化是什么意思
  • 网站开发的开题报告模板打广告
  • 巢湖市网站建设优化成品网站货源1688在线
  • 供别人采集的网站怎么做网页设计制作网站教程
  • 天津建委网站 官网易推广
  • 千锋教育培训多少钱津seo快速排名
  • 成都公司网站seo搜索优化指的是什么
  • 哪里做网站比较号长春网站优化平台
  • 新做的网站如何备案免费网站流量
  • 网站备案被注销吗外贸公司如何做推广
  • 网站正在建设页面推广之家
  • 上海心橙科技网站建设服务器域名查询
  • 南宁建企业网站公司线上销售平台如何推广
  • 厦门网站综合优化贵吗seo站内优化培训
  • 做网站用的什么服务器seo站长平台
  • phpcms多个网站公司推广发帖网站怎么做
  • dedecms网站地图模板搜索关键词排名
  • 中建八局一公司待遇怎么样电脑优化是什么意思
  • 桃花岛网站是什么it培训班出来工作有人要么
  • 网站做优化得话从哪里优化在线seo短视频
  • 购物网站备案费用网络营销课程思政
  • 影视自助建站系统源码新闻摘抄2022最新20篇
  • 幼儿园网站模板怎么做百度百科入口