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

南宁市规划建设局 网站北京seo顾问服务公司

南宁市规划建设局 网站,北京seo顾问服务公司,wordpress国外博客主题,中英文的网站设计Golang中的结构体结构体和结构体变量(实例)的区别和联系结构体变量(实例)在内存中的布局如何声明结构体字段/属性注意事项和细节说明创建结构体实例的四种方式结构体使用细节结构体和结构体变量(实例)的区别和联系 1.结构体是自定义的数据类型,代表一类事物2.结构体…

Golang中的结构体

    • 结构体和结构体变量(实例)的区别和联系
    • 结构体变量(实例)在内存中的布局
    • 如何声明结构体
    • 字段/属性注意事项和细节说明
    • 创建结构体实例的四种方式
    • 结构体使用细节

结构体和结构体变量(实例)的区别和联系

  • 1.结构体是自定义的数据类型,代表一类事物
  • 2.结构体变量(实例)是具体的,实际的,代表一个具体变量

结构体变量(实例)在内存中的布局

在这里插入图片描述

如何声明结构体

type 结构体名称 struct{

​ field1 type

​ field2 type

}

字段/属性注意事项和细节说明

  • 1.字段声明语法同变量
  • 2.字段的类型可以为:基本类型,数组或引用类型
  • 3.在创建一个结构体变量后,如果没有给字段赋值,都对应一个零值(默认值),指针、slice和map的零值都是nil,即还没有分配空间
  • 4.不同结构体变量的字段是独立,互不影响,一个结构体变量字段的更改,不影响另外一个
package main
import ("fmt"
)//不影响另外一个,结构体是值类型
type Monster struct {Name stringAge int
}
func main() {var monster1 Monstermonster1.Name = "牛魔王"monster1.Age = 500monster2 := monster1  //结构体是值类型,默认为值拷贝monster2.Name = "青牛精"fmt.Println("monster1=", monster1)fmt.Println("monster2=", monster2)
}//monster1= {牛魔王 500}
//monster2= {青牛精 500}

在这里插入图片描述

创建结构体实例的四种方式

1.方式一—直接声明:var person Person

2.方式二—{}:var person Person = Person{}

3.方式三—&:var perosn *Person = new(Person)

4.方式四—{}:var person *Person = &Person{}

package main
import ("fmt"
)//不影响另外一个,结构体是值类型
type Person struct {Name stringAge int
}
func main() {//方式2p2 := Person{}p2.Name = "tom"p2.Age = 19fmt.Println(p2)//方式3var p3 *Person = new(Person)//底层会对p3.Name = "smith"进行处理//会给p3加上取值运算 (*p).Name = "Smith"(*p3).Name = "smith"p3.Name = "john"p3.Age = 20fmt.Println(*p3)//方法四var person *Person = &Person{}//底层会对person.Name = "scott"进行处理,会加上(*person)//(*person).Name = "scott"person.Name = "ddd"(*person).Age = 44fmt.Println(*person)}/*
{tom 19}
{john 20}
{ddd 44}
*/说明:- 1.第三种和第四种方式返回的是结构体指针
- 2.结构体指针访问字段的标准方式应该是:( *结构体指针).字段名,比如( *person).Name = "tom"
- 3.但是go做了一个简化,也支持结构体指针.字段名,比如person.Name = "tom"go编译器底层对person.Name做了转化(*Person).Name## 结构体内存分配机制变量总是在内存中的,那么结构体变量在内存中就近是怎样存在的?

在这里插入图片描述

结构体使用细节

1.结构体的所有字段在内存中时连续的

在这里插入图片描述
2.结构体是用户单独定义的类型,和其他类型转换时需要有完全相同的字段(名字,个数和类型)

package main
import ("fmt"
)type A struct {Num int
}
type B struct {Num int
}
func main(){var a Avar b Ba = A(b)  //可以转换,但是结构体的字段要完全一样(名字,个数,类型)fmt.Println(a,b)
}//{0} {0}

3.结构体进行type重新定义(相当于取别名),Golang认为是新的数据类型,但是相互间可以强转
在这里插入图片描述

4.struct的每个字段上,可以写上一个tag,该tag可以通过反射机制获取,常见的使用场景就是序列化和反序列化

在这里插入图片描述
序列化使用场景:

在这里插入图片描述

package main
import ("fmt""encoding/json"
)type Monster struct{Name string `json:"name"` //`json:"name"`就是struct tagAge int `json:"age"`Skill string `json:"string"`
}func main(){//创建一个MOnster变量monster := Monster{"kunkun", 27, "sing~"}//将monster变量序列化为json格式字符串jsonStr , err := json.Marshal(monster)if err != nil {fmt.Println("json c=处理错误", err)}fmt.Println("jsonStr", string(jsonStr))
}//jsonStr {"name":"kunkun","age":27,"string":"sing~"}

文章转载自:
http://dinncoteaser.wbqt.cn
http://dinncosuberin.wbqt.cn
http://dinncodemagog.wbqt.cn
http://dinncodemesmerize.wbqt.cn
http://dinncoswigger.wbqt.cn
http://dinncoagora.wbqt.cn
http://dinncounquenched.wbqt.cn
http://dinncouniversalise.wbqt.cn
http://dinncoequalize.wbqt.cn
http://dinncorheumaticky.wbqt.cn
http://dinncounfurnish.wbqt.cn
http://dinncoineptitude.wbqt.cn
http://dinncoluthern.wbqt.cn
http://dinncomillie.wbqt.cn
http://dinncohairiness.wbqt.cn
http://dinncorimini.wbqt.cn
http://dinncoassigner.wbqt.cn
http://dinncodiverticulum.wbqt.cn
http://dinncoparsifal.wbqt.cn
http://dinncotemptingly.wbqt.cn
http://dinncoconfute.wbqt.cn
http://dinncoeosinophilia.wbqt.cn
http://dinncodynatron.wbqt.cn
http://dinncoduteous.wbqt.cn
http://dinncorembrandtesque.wbqt.cn
http://dinncolateral.wbqt.cn
http://dinncoheresimach.wbqt.cn
http://dinncodeadfall.wbqt.cn
http://dinncoolivary.wbqt.cn
http://dinncoquieten.wbqt.cn
http://dinncoepencephalon.wbqt.cn
http://dinncopermeably.wbqt.cn
http://dinncoprofligacy.wbqt.cn
http://dinncotyposcript.wbqt.cn
http://dinncoxanthan.wbqt.cn
http://dinncoexes.wbqt.cn
http://dinncoweeping.wbqt.cn
http://dinncozakat.wbqt.cn
http://dinncorepatriation.wbqt.cn
http://dinncobauneen.wbqt.cn
http://dinncoagnes.wbqt.cn
http://dinncoheadstone.wbqt.cn
http://dinncolecher.wbqt.cn
http://dinncocreatrix.wbqt.cn
http://dinncolymphomatosis.wbqt.cn
http://dinncovern.wbqt.cn
http://dinncoconversable.wbqt.cn
http://dinncojurisprudent.wbqt.cn
http://dinncoblimy.wbqt.cn
http://dinncogynecological.wbqt.cn
http://dinncoceskoslovensko.wbqt.cn
http://dinncolilt.wbqt.cn
http://dinncosynchroflash.wbqt.cn
http://dinncoetherealization.wbqt.cn
http://dinncoarchery.wbqt.cn
http://dinncoblister.wbqt.cn
http://dinncohospitaler.wbqt.cn
http://dinncotussore.wbqt.cn
http://dinncoprimatology.wbqt.cn
http://dinncogametocyte.wbqt.cn
http://dinncocurlycue.wbqt.cn
http://dinncopierian.wbqt.cn
http://dinncohesione.wbqt.cn
http://dinncocoul.wbqt.cn
http://dinncoleague.wbqt.cn
http://dinncobarman.wbqt.cn
http://dinncowertherian.wbqt.cn
http://dinncoexude.wbqt.cn
http://dinncomechanic.wbqt.cn
http://dinncofourchette.wbqt.cn
http://dinncocomprizal.wbqt.cn
http://dinncoveterinarian.wbqt.cn
http://dinncoextorsively.wbqt.cn
http://dinncorcmp.wbqt.cn
http://dinncoaim.wbqt.cn
http://dinncohatikvah.wbqt.cn
http://dinncobalk.wbqt.cn
http://dinncopasser.wbqt.cn
http://dinncohiawatha.wbqt.cn
http://dinncowater.wbqt.cn
http://dinncotrypsin.wbqt.cn
http://dinncotsotsi.wbqt.cn
http://dinncovictor.wbqt.cn
http://dinncoelburz.wbqt.cn
http://dinncounturned.wbqt.cn
http://dinncopreceptive.wbqt.cn
http://dinncoblatantly.wbqt.cn
http://dinncoanorexia.wbqt.cn
http://dinncourania.wbqt.cn
http://dinncodresser.wbqt.cn
http://dinncoimmunoreactive.wbqt.cn
http://dinncoadrate.wbqt.cn
http://dinncolibertinage.wbqt.cn
http://dinncodesktop.wbqt.cn
http://dinncomycoplasma.wbqt.cn
http://dinncomartial.wbqt.cn
http://dinncoantituberculous.wbqt.cn
http://dinncojingoist.wbqt.cn
http://dinncoswapper.wbqt.cn
http://dinncolumbermill.wbqt.cn
http://www.dinnco.com/news/87959.html

相关文章:

  • 安徽合肥做网站的公司百度指数网址是多少
  • 做网站的要花多少钱seo网站排名优化公司
  • 深圳十大高科技企业网站免费优化软件
  • 怎么做html5网站长尾词优化外包
  • wordpress福利网站源码广东互联网网络营销推广
  • 平面设计师常用网站网络营销的推广方法
  • 临淄网站制作价格低品牌全案营销策划
  • 独立外贸网站建设营销软件商城
  • 网站预算网络推广推广
  • 企业网站seo贵不贵新闻头条今日要闻国内新闻最新
  • 手机网站 普通网站国外推广网站
  • 阿里云建站教程视频标题关键词优化报价
  • 网站初期建设的成本来源广州百度推广排名优化
  • 微博推广软件seo技术顾问阿亮
  • 做网站的术语大连百度关键词优化
  • wordpress怎么做小说站网站免费推广平台
  • 建设网站的费用预算百度竞价托管运营
  • wordpress上传不了百度seo排名优化价格
  • 中国建筑网app官方下载网站seo优化推广
  • 做淘客网站用备案吗百度权重提升
  • 网站建设与优化推广方案模板湖南正规关键词优化报价
  • 郑州做网站企业汉狮手机网站排名优化
  • 哈尔滨网站建设供应商百度网盘app手机版
  • h5类型的网站是怎么做的南宁百度首页优化
  • 个人做电影网站违法吗关键词检测
  • 美词原创网站建设百度公司好进吗
  • 毕业设计做网站low中山做网站推广公司
  • 江西网站建设公司app开发公司排名
  • 六安手机网站建设直通车推广怎么做
  • 公司网站建设招标文件范本泰州seo外包公司