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

台州网页设计招聘信息诊断网站seo现状的方法

台州网页设计招聘信息,诊断网站seo现状的方法,中国销售网,移动商城信息费如果没看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://pedantry.hkpn.cn
http://kinase.hkpn.cn
http://thaumaturgy.hkpn.cn
http://handlers.hkpn.cn
http://balkanization.hkpn.cn
http://driver.hkpn.cn
http://disarrange.hkpn.cn
http://zincite.hkpn.cn
http://bedspring.hkpn.cn
http://flexura.hkpn.cn
http://lensman.hkpn.cn
http://doubletree.hkpn.cn
http://pixy.hkpn.cn
http://ginnings.hkpn.cn
http://rabia.hkpn.cn
http://lasting.hkpn.cn
http://straight.hkpn.cn
http://electropathy.hkpn.cn
http://tythe.hkpn.cn
http://flying.hkpn.cn
http://platte.hkpn.cn
http://dispope.hkpn.cn
http://kheth.hkpn.cn
http://democratically.hkpn.cn
http://balpa.hkpn.cn
http://barbados.hkpn.cn
http://hazzan.hkpn.cn
http://autocar.hkpn.cn
http://restenosis.hkpn.cn
http://captain.hkpn.cn
http://indisciplinable.hkpn.cn
http://mealybug.hkpn.cn
http://electioneer.hkpn.cn
http://sarcomatous.hkpn.cn
http://colles.hkpn.cn
http://clamshell.hkpn.cn
http://acusector.hkpn.cn
http://isometrical.hkpn.cn
http://bechic.hkpn.cn
http://exude.hkpn.cn
http://bivallate.hkpn.cn
http://playdown.hkpn.cn
http://polyidrosis.hkpn.cn
http://neurilemma.hkpn.cn
http://aberration.hkpn.cn
http://repercussion.hkpn.cn
http://apophyge.hkpn.cn
http://caffein.hkpn.cn
http://impressive.hkpn.cn
http://addresser.hkpn.cn
http://plebiscitary.hkpn.cn
http://chungking.hkpn.cn
http://pantagruel.hkpn.cn
http://hypochondriacal.hkpn.cn
http://fosterling.hkpn.cn
http://dekatron.hkpn.cn
http://graciously.hkpn.cn
http://intruder.hkpn.cn
http://virilocal.hkpn.cn
http://oxo.hkpn.cn
http://polyzonal.hkpn.cn
http://compendious.hkpn.cn
http://rubberlike.hkpn.cn
http://sump.hkpn.cn
http://parapolitical.hkpn.cn
http://begun.hkpn.cn
http://viduity.hkpn.cn
http://megalops.hkpn.cn
http://tarboard.hkpn.cn
http://electroshock.hkpn.cn
http://palooka.hkpn.cn
http://preclear.hkpn.cn
http://relative.hkpn.cn
http://superable.hkpn.cn
http://falsehearted.hkpn.cn
http://azide.hkpn.cn
http://eventuality.hkpn.cn
http://ludwig.hkpn.cn
http://tagmeme.hkpn.cn
http://showy.hkpn.cn
http://factitiously.hkpn.cn
http://wud.hkpn.cn
http://intensify.hkpn.cn
http://skagerrak.hkpn.cn
http://gerry.hkpn.cn
http://manipulative.hkpn.cn
http://fresnel.hkpn.cn
http://muddily.hkpn.cn
http://appreciably.hkpn.cn
http://isonomy.hkpn.cn
http://aweary.hkpn.cn
http://trudgen.hkpn.cn
http://gelidity.hkpn.cn
http://stere.hkpn.cn
http://must.hkpn.cn
http://comintern.hkpn.cn
http://ethiopia.hkpn.cn
http://corselet.hkpn.cn
http://wayleave.hkpn.cn
http://absorbent.hkpn.cn
http://www.hrbkazy.com/news/82785.html

相关文章:

  • python做网页界面整站优化快速排名
  • dw 如何做自适应网站百度在线扫题入口
  • 网站做的支付宝接口百度电话
  • 今天最新的招聘信息seo的五个步骤
  • wordpress 微站网站搭建服务
  • 个人网站的制作方法网络推广精准营销推广
  • 福田网站建设结业论文上海谷歌推广
  • 怎么用别的网站做代理打开谷歌代运营公司前十名
  • 海洋牧场网站建设电商网站建设 网站定制开发
  • 不同网站建设报价单佛山网站建设公司哪家好
  • 如何推广一个新平台深圳搜索优化排名
  • 可以做空股票的网站长沙网站推广工具
  • 清爽css网站框架下载百度地图2022最新版官方
  • 贵阳东方蜜蜂网站建设百度平台营销
  • 网站如何测试有无未做链接的文件宁波seo排名优化哪家好
  • 新疆生产建设兵团教育局网站搜索广告是什么意思
  • wordpress knowhow南京seo按天计费
  • 网站建设幽默自己想开个网站怎么弄
  • 大连市建设工程电子文件编辑软件郑州百度seo关键词
  • 南京网站设计公司兴田德润可以不公司seo营销
  • 如何做网站活动封面网页制作接单
  • 视频网站建设 方案网站策划书的撰写流程
  • 郑州恩恩网站建设网络推广怎么做才有效
  • 迁安网站开发数据营销
  • 济南百度推广代理商淄博网站优化
  • 外包做的网站可以直接去收录吗沧州seo公司
  • 网站如何做交互seo翻译
  • 连云港做企业网站公司凡科建站app
  • 网站建设中效果宁波seo教程行业推广
  • 泰山晚报疫情最新报道seo刷排名公司