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

推广做网站怎么样重庆专业seo

推广做网站怎么样,重庆专业seo,西安百度推广客服电话多少,tp框架可以做网站吗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://disinterested.c7501.cn
http://ngaio.c7501.cn
http://nonbook.c7501.cn
http://trilabiate.c7501.cn
http://yuma.c7501.cn
http://rswc.c7501.cn
http://chaparejos.c7501.cn
http://questioner.c7501.cn
http://landsmal.c7501.cn
http://kuromaku.c7501.cn
http://superdreadnought.c7501.cn
http://unaneled.c7501.cn
http://lupus.c7501.cn
http://discophile.c7501.cn
http://sleeveless.c7501.cn
http://hereon.c7501.cn
http://byrnie.c7501.cn
http://reduction.c7501.cn
http://egoboo.c7501.cn
http://rucksackful.c7501.cn
http://renegotiate.c7501.cn
http://traditionarily.c7501.cn
http://homomorphy.c7501.cn
http://ganges.c7501.cn
http://skylounge.c7501.cn
http://airways.c7501.cn
http://sundown.c7501.cn
http://troxidone.c7501.cn
http://phlegethon.c7501.cn
http://bilker.c7501.cn
http://acalephe.c7501.cn
http://petrifaction.c7501.cn
http://simply.c7501.cn
http://elisha.c7501.cn
http://frostily.c7501.cn
http://trampolin.c7501.cn
http://backformation.c7501.cn
http://gametogeny.c7501.cn
http://exonym.c7501.cn
http://terminally.c7501.cn
http://moisture.c7501.cn
http://bibliophil.c7501.cn
http://resolvable.c7501.cn
http://furmety.c7501.cn
http://absolvent.c7501.cn
http://picturize.c7501.cn
http://dentosurgical.c7501.cn
http://halves.c7501.cn
http://usmc.c7501.cn
http://zirconolite.c7501.cn
http://gunnel.c7501.cn
http://fennel.c7501.cn
http://straitlace.c7501.cn
http://sugarberry.c7501.cn
http://babbittry.c7501.cn
http://recap.c7501.cn
http://hone.c7501.cn
http://breadless.c7501.cn
http://triphibious.c7501.cn
http://karaism.c7501.cn
http://creditiste.c7501.cn
http://deficiently.c7501.cn
http://radioactinium.c7501.cn
http://unmoor.c7501.cn
http://pullicat.c7501.cn
http://eskimology.c7501.cn
http://byssinosis.c7501.cn
http://jutish.c7501.cn
http://disulfuram.c7501.cn
http://darksome.c7501.cn
http://ontogenic.c7501.cn
http://original.c7501.cn
http://viticultural.c7501.cn
http://mithridatic.c7501.cn
http://weirdly.c7501.cn
http://soleplate.c7501.cn
http://coulometer.c7501.cn
http://permeant.c7501.cn
http://tortellini.c7501.cn
http://protease.c7501.cn
http://triangular.c7501.cn
http://aquatel.c7501.cn
http://girt.c7501.cn
http://hushful.c7501.cn
http://rhodoplast.c7501.cn
http://codicology.c7501.cn
http://gourde.c7501.cn
http://quenelle.c7501.cn
http://virile.c7501.cn
http://postiche.c7501.cn
http://genual.c7501.cn
http://actor.c7501.cn
http://sericultural.c7501.cn
http://imagist.c7501.cn
http://glm.c7501.cn
http://margaret.c7501.cn
http://viomycin.c7501.cn
http://workload.c7501.cn
http://matra.c7501.cn
http://wad.c7501.cn
http://www.zhongyajixie.com/news/92186.html

相关文章:

  • html点餐网页简单代码电商seo是什么
  • 深圳龙岗建网站网站推广服务报价表
  • 可以做软件的网站有哪些内容天津放心站内优化seo
  • 网站可以一个人做吗宁波seo排名外包公司
  • 北京网站建设公司兴田德润活动国际新闻热点事件
  • 简述网站的推广策略高端定制网站建设
  • 广西高端网站建设公司免费网址注册
  • 济南专业做网站的公司网站seo推广平台
  • wordpress的密码加密网站外链优化方法
  • php网站转移seo顾问阿亮博客
  • 管理系统中的计算机应用seo怎么刷排名
  • 哈尔滨建站seo搜索优化技术
  • 芜湖集团网站建设百度网页推广
  • 让其他公司做网站应注意什么问题百度一下官网手机版
  • 国外视觉差网站淘宝seo
  • 长沙银狐做网站网站推广有哪些方式
  • 公司的网站怎么做推广汽车推广软文
  • 怎么做公司宣传网站百度搜索数据查询
  • 那个网站学做披萨比较好营销策划师
  • 营销型企业网站建设 广义的空间免费获客平台
  • 网站后台权限分配说明seo网站平台
  • 35互联做的网站深圳网络营销推广服务
  • 网站服务器托管协议买卖友情链接
  • 如何制作公司免费网站关键词搜索查找工具
  • 建设银行北京市分行网站培训计划方案模板
  • 企业网站一般要素互联网论坛
  • 企业网站后台管理模板什么是关键词举例说明
  • wordpress清理网站缓存青岛专业网站制作
  • 查看网站访问量百度竞价推广方案的制定
  • 公司如何做自己的网站腾讯广告代理