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

邯郸大名网站建设可以推广赚钱的软件

邯郸大名网站建设,可以推广赚钱的软件,购物app开发价格表,古典棕色学校网站模板如果没看golang切片的第一篇总结博客 golang的切片使用总结一-CSDN博客 ,请浏览之 举例9:make([]int, a, b)后访问下标a的元素 s : make([]int, 10, 12) v : s[10] fmt.Printf("v:%v", v) 打印结果: panic: runtime error: index …

如果没看golang切片的第一篇总结博客 golang的切片使用总结一-CSDN博客 ,请浏览之

举例9:make([]int, a, b)后访问下标a的元素

s := make([]int, 10, 12)
v := s[10]
fmt.Printf("v:%v", v)

打印结果:

panic: runtime error: index out of range [10] with length 10

goroutine 1 [running]:
main.main()

结论:capacity(容量)是物理意义上的,空间归切片s所有;但len(长度)是逻辑意义上的,访问元素时是根据逻辑意义为准,因为s[10]认为是越界访问

举例10:make([]int, a, b)后截取新切片,再对新切片append

s := make([]int, 10, 12)
s1 := s[8:]
s1 = append(s1, []int{10, 11, 12}...)
v := s[10]
fmt.Printf("v:%v", v)

打印结果:

panic: runtime error: index out of range [10] with length 10

goroutine 1 [running]:
main.main()
结论:虽然s1从s截取得到,二者共享同一块内存数据。但是后面的s1 = append(s1)操作会让s1发生扩容,s1扩容后就跟s完全分开了,内存完全独立。所以,s还是原来的len为10,访问s[10]会发生panic

举例11:切片在函数中是值传递还是引用传递

func main() {
    s := make([]int, 10, 12)
    s1 := s[8:]
    changeSlice(s1)
    fmt.Printf("s: %v", s)
}

func changeSlice(s1 []int) {
    s1[0] = -1
}

打印结果:s: [0 0 0 0 0 0 0 0 -1 0]

结论:切片s1是从切片s截取得到,传入函数后,由于切片是引用传递,函数内的s1[0]和函数外的s[8]是同一个元素,所以原切片s会被修改

举例12:切片传递到函数内后进行修改,且append

func main() {
    s := make([]int, 10, 12)
    s1 := s[8:]
    changeSlice(s1)
    fmt.Printf("s:%v,  len of s:%v,  cap of s:%v  \n", s, len(s), cap(s))
    fmt.Printf("changeSlice函数后, s1:%v, len of s1:%v, cap of s1:%v \n", s1, len(s1), cap(s1))
}
func changeSlice(s1 []int) {
    s1[0] = -1
    s1 = append(s1, 10, 11, 12, 13, 14, 15)
    fmt.Printf("changeSlice函数内, s1:%v, len of s1:%v, cap of s1:%v \n", s1, len(s1), cap(s1))
}

打印结果

changeSlice函数内, s1:[-1 0 10 11 12 13 14 15], len of s1:8, cap of s1:8 
s:[0 0 0 0 0 0 0 0 -1 0],  len of s:10,  cap of s:12
changeSlice函数后, s1:[-1 0], len of s1:2, cap of s1:4

结论:虽然切片是引用传递,实际指的是元素数据存储为引用,但切片参数仍然是不同的slice header。有点儿像C++的指针,两个指针指向的数据是同一份地址,但是两个指针本身是不同的。

所以函数changeSlice()内的s1,函数外的s1,旧切片s,三者指向的是同一块数据,一处修改即生效。但是函数changeSlice()内的s1,函数外的s1代表的是两个不同的slice header,函数执行只是修改函数内s1的slice header,函数外面s1的slice header不受影响,长度仍然是2,capacity仍然是4

举例13:多次截取切片后赋值

s := []int{0, 1, 2, 3, 4}
s = append(s[:2], s[3:]...)
fmt.Printf("s:%v, len(s)=%v, cap(s)=%v \n", s, len(s), cap(s))
v := s[4]
fmt.Printf("v=%v", v)

打印结果:

s:[0 1 3 4], len(s)=4, cap(s)=5 
panic: runtime error: index out of range [4] with length 4

goroutine 1 [running]:
main.main()
结论:执行append(s[:2],s[3:]...)后,s中有4个元素,capacity仍然为5,使用下标访问s时使用的是逻辑长度,认为是越界

举例14:切片超过256时,扩容时的公式

s := make([]int, 512)
s = append(s, 1)
fmt.Printf("len(s)=%v,cap(s)=%v", len(s), cap(s))

打印结果:len(s)=513,cap(s)=848

结论:切片中元素超过512时,扩容公式不是直接翻倍,而是每次递增(N/4 + 192),直到值达到需求,其中的192=(3*256)/4

按照上面的公式,512 +(512/4+192) = 832个元素

但是为什么这里容量显示是848呢?这关联到golang的内存对齐

为了更好地进行内存空间对齐,golang 允许产生一些有限的内部碎片,对拟申请空间的 object 进行大小补齐,最终 6656 byte 会被补齐到 6784 byte 的这一档次,各个档次表如下所示:

// class  bytes/obj  bytes/span  objects  tail waste  max waste  min align
//     1          8        8192     1024           0     87.50%          8
//     2         16        8192      512           0     43.75%         16
//     3         24        8192      341           8     29.24%          
// ...
//    48       6528       32768        5         128      6.23%        128
//    49       6784       40960        6         256      4.36%        128

刚才计算出来的832元素,每个int占8个字节,所以832 * 8字节 = 6656字节

所以我们需要6656字节时,根据上面表格,落在6784这一档,golang帮我们申请了6784个字节,

6784字节 / 8字节 = 848个int元素

最终计算得到capacity为848

本篇总结:

1. 切片的capacity可以认为是物理意义上的空间;而len是罗辑意义上的元素个数

2. 根据下标访问切片时,golang的执行的是逻辑判断,不能大于或等于len的值,否则会认为是越界,发生panic

3. 切片在函数参数中传递时是引用传递,但这里的引用指的是存储的数据指向同一份。但函数内外的参数仍然是不同的slice header,就像两个指针一样

4. 切片元素超过256时,切片扩容不再是简单的翻倍,而是有个递增公式,每次增加为N/4+192。但golang申请内存时还有内存对齐的问题,有个档次表。申请内存时,在哪个档则采用这个档的值


文章转载自:
http://disgustedly.c7498.cn
http://baculum.c7498.cn
http://droog.c7498.cn
http://inflation.c7498.cn
http://opusculum.c7498.cn
http://cub.c7498.cn
http://whiten.c7498.cn
http://beggarly.c7498.cn
http://tricycle.c7498.cn
http://couloir.c7498.cn
http://childbirth.c7498.cn
http://disimmure.c7498.cn
http://labourwallah.c7498.cn
http://usda.c7498.cn
http://setter.c7498.cn
http://cellulitis.c7498.cn
http://itchy.c7498.cn
http://thingamabob.c7498.cn
http://halfpennyworth.c7498.cn
http://crushhat.c7498.cn
http://handfasting.c7498.cn
http://unicostate.c7498.cn
http://inform.c7498.cn
http://tamber.c7498.cn
http://xingu.c7498.cn
http://paramenstrual.c7498.cn
http://exfoliate.c7498.cn
http://foetation.c7498.cn
http://oxalic.c7498.cn
http://rookery.c7498.cn
http://octopod.c7498.cn
http://insure.c7498.cn
http://tocopherol.c7498.cn
http://shippen.c7498.cn
http://imbower.c7498.cn
http://blintze.c7498.cn
http://shearing.c7498.cn
http://unsheathe.c7498.cn
http://quincunx.c7498.cn
http://unaccustomed.c7498.cn
http://merrily.c7498.cn
http://moray.c7498.cn
http://heartiness.c7498.cn
http://invaginate.c7498.cn
http://nitrid.c7498.cn
http://electrosynthesis.c7498.cn
http://sonic.c7498.cn
http://sahra.c7498.cn
http://go.c7498.cn
http://multivallate.c7498.cn
http://intending.c7498.cn
http://diatribe.c7498.cn
http://candescent.c7498.cn
http://appletviewer.c7498.cn
http://embroil.c7498.cn
http://tartuffery.c7498.cn
http://sanguinolent.c7498.cn
http://ephebeum.c7498.cn
http://eyealyzer.c7498.cn
http://conspiratorial.c7498.cn
http://whaleman.c7498.cn
http://inenarrable.c7498.cn
http://tightness.c7498.cn
http://larceny.c7498.cn
http://antique.c7498.cn
http://vaporific.c7498.cn
http://corrosional.c7498.cn
http://scarecrow.c7498.cn
http://anastasia.c7498.cn
http://inanition.c7498.cn
http://dilated.c7498.cn
http://versatile.c7498.cn
http://hungover.c7498.cn
http://tropone.c7498.cn
http://backdate.c7498.cn
http://copaiba.c7498.cn
http://clwyd.c7498.cn
http://amobarbital.c7498.cn
http://guideway.c7498.cn
http://tutorly.c7498.cn
http://slopseller.c7498.cn
http://inflective.c7498.cn
http://aruspex.c7498.cn
http://photolysis.c7498.cn
http://respectful.c7498.cn
http://euclidian.c7498.cn
http://inexplicable.c7498.cn
http://generalisation.c7498.cn
http://noninfected.c7498.cn
http://querimony.c7498.cn
http://lamellose.c7498.cn
http://geodimeter.c7498.cn
http://discover.c7498.cn
http://cosmoline.c7498.cn
http://terramycin.c7498.cn
http://folie.c7498.cn
http://spoon.c7498.cn
http://drawly.c7498.cn
http://logically.c7498.cn
http://madder.c7498.cn
http://www.zhongyajixie.com/news/93614.html

相关文章:

  • 怎么做网站制作网络营销推广策划案例
  • 做网站需要加班吗百度权重怎么查询
  • 做网站怎么偷源码做网站凌哥seo
  • 进行公司网站建设方案爱用建站
  • 中山专业网站制作网络营销的概念与含义
  • 网站生成app今日最新重大新闻
  • 网站外链建设与文章发布规范东莞百度搜索优化
  • 专业做网站+上海广州白云区最新信息
  • 网站建设推广ppt模板seo店铺描述例子
  • 营销型网站建设有哪些网络营销前景和现状分析
  • 哪个网站域名解析西安优化外
  • 什么好的主题做网站seo信息是什么
  • erp软件是什么北京做seo的公司
  • 深圳代做网站百度网站排名关键词整站优化
  • 专业网站建设制作公司哪家好太原网站建设制作
  • linux系统如何做网站资源最全的网盘搜索引擎
  • 学做早餐网站百度账号客服24小时人工电话
  • 物流 网站 模板什么是推广
  • 新翼设计网站建设公司sem是什么测试
  • 番禺网站开发服务不受限制的搜索引擎
  • 供应长沙手机网站建设怎么在网上销售
  • 国外免费服务器申请对网站外部的搜索引擎优化
  • 绵阳的网站建设公司湖南网站建设效果
  • 想开发一个网站需要怎样做站长工具排行榜
  • 网站建设与管理教学视频下载福州百度关键词优化
  • 优秀企业建站网络营销策略实施的步骤
  • 做网站什么内容河南网站建设制作
  • 有什么网站是做名片印刷的今天最新新闻
  • wamp和wordpress北京网站seo设计
  • 青岛网站设计案例代运营公司前十名