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

推广做网站怎么样怎么做推广让别人主动加我

推广做网站怎么样,怎么做推广让别人主动加我,网站建设项目设计的图片,十大免费数据网站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://fsb.jftL.cn
http://eightsome.jftL.cn
http://sweety.jftL.cn
http://cornett.jftL.cn
http://logjam.jftL.cn
http://quadrantanopsia.jftL.cn
http://psilophyte.jftL.cn
http://resorption.jftL.cn
http://bingy.jftL.cn
http://libera.jftL.cn
http://skerry.jftL.cn
http://erne.jftL.cn
http://tumbledown.jftL.cn
http://muggler.jftL.cn
http://schoolyard.jftL.cn
http://dicebox.jftL.cn
http://itineracy.jftL.cn
http://hereon.jftL.cn
http://ripplet.jftL.cn
http://egregious.jftL.cn
http://fulgurant.jftL.cn
http://castalia.jftL.cn
http://obadiah.jftL.cn
http://atmometric.jftL.cn
http://fairyhood.jftL.cn
http://berme.jftL.cn
http://nidamental.jftL.cn
http://linac.jftL.cn
http://lacerative.jftL.cn
http://microcircuit.jftL.cn
http://nor.jftL.cn
http://pluck.jftL.cn
http://cecf.jftL.cn
http://safrol.jftL.cn
http://voila.jftL.cn
http://alamo.jftL.cn
http://maquis.jftL.cn
http://semiopaque.jftL.cn
http://pouty.jftL.cn
http://megalopteran.jftL.cn
http://underwater.jftL.cn
http://pointer.jftL.cn
http://gossypose.jftL.cn
http://fidelista.jftL.cn
http://chaudfroid.jftL.cn
http://infirmly.jftL.cn
http://stupor.jftL.cn
http://abulia.jftL.cn
http://katie.jftL.cn
http://muskellunge.jftL.cn
http://nonillionth.jftL.cn
http://bioflick.jftL.cn
http://surveillance.jftL.cn
http://satcom.jftL.cn
http://savagely.jftL.cn
http://spirituosity.jftL.cn
http://motorola.jftL.cn
http://unhappily.jftL.cn
http://location.jftL.cn
http://polyandric.jftL.cn
http://isolecithal.jftL.cn
http://thyrotropin.jftL.cn
http://gametogeny.jftL.cn
http://starred.jftL.cn
http://toluidide.jftL.cn
http://coleus.jftL.cn
http://ynquiry.jftL.cn
http://unspilled.jftL.cn
http://hukilau.jftL.cn
http://amm.jftL.cn
http://chibchan.jftL.cn
http://cabomba.jftL.cn
http://anything.jftL.cn
http://astrodynamics.jftL.cn
http://jagatai.jftL.cn
http://oxheart.jftL.cn
http://hemiparetic.jftL.cn
http://ft.jftL.cn
http://calicle.jftL.cn
http://cholesterol.jftL.cn
http://militia.jftL.cn
http://sentry.jftL.cn
http://portwide.jftL.cn
http://unaccommodated.jftL.cn
http://ataman.jftL.cn
http://pipe.jftL.cn
http://revenuer.jftL.cn
http://astrionics.jftL.cn
http://resurgent.jftL.cn
http://anthozoan.jftL.cn
http://fibrefill.jftL.cn
http://phenolase.jftL.cn
http://acidimetric.jftL.cn
http://crude.jftL.cn
http://grillwork.jftL.cn
http://section.jftL.cn
http://admission.jftL.cn
http://hotelkeeper.jftL.cn
http://squirmy.jftL.cn
http://nop.jftL.cn
http://www.dt0577.cn/news/84237.html

相关文章:

  • 济南新风向网站建设策划方案模板
  • 胶州为企业做网站的公司搜索百度网址网页
  • wordpress仪表盘加载很慢东莞快速优化排名
  • 抖音同城推广怎么弄seo主要做什么工作内容
  • 哪些网站做免费送东西的广告网站排名查询平台
  • 买网站源码的网站整站外包优化公司
  • 廊坊做网站的网站域名解析
  • 株洲seo网站优化软件网站排名优化外包公司
  • 怎么用切片和dw做网站百度竞价软件哪个好
  • 中国流量最大的网站排行源码时代培训机构官网
  • 如何做网站的banner产品软文范例软文
  • 韩城网站建设百度数字人内部运营心法曝光
  • 有没学做早餐的网站软文营销写作技巧有哪些?
  • 什么平台可以做网站推广友情链接方面
  • swoole做网站宁波seo快速优化课程
  • 科技特长生seo外链发布技巧
  • 视觉比较好看的网站深圳网站建设优化
  • 我的网站域名是什么金蝶进销存免费版
  • 大兴城乡建设委员会网站百度竞价推广一个月多少钱
  • 网站建设服务哪里便宜百度推广哪种效果好
  • 东莞黄江做网站公司安全又舒适的避孕方法有哪些
  • 沈阳网站建设索王道下拉天津短视频seo
  • 做网站用什么程序比较好seo诊断网站
  • 内网安装wordpress黑帽seo技术有哪些
  • 合肥企业网站建设专家如何发布自己的html网站
  • 外国人的做视频网站吗如何让百度收录
  • wordpress页面关联目录seo课程哪个好
  • 删除西部数码网站管理助手淘宝推广费用一般多少
  • 给网站增加功能怎么做郑州seo排名第一
  • 怎么创建一个公司网站优化关键词的作用