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

可以做360度全景图的网站赣州seo

可以做360度全景图的网站,赣州seo,衡器行业网站建设模板,河南建筑工程有限公司空切片与nil切片有区别吗? 很多开发人员经常混淆nil切片和空切片,不清楚什么时候使用空切片什么时候使用nil,而有些库函数又对这两者使用进行了区分。下面先来看看它们的定义。 空切片是length为0的切片当切片等于nil时为nil切片 下面是几种不同空切片…
空切片与nil切片有区别吗?

很多开发人员经常混淆nil切片和空切片,不清楚什么时候使用空切片什么时候使用nil,而有些库函数又对这两者使用进行了区分。下面先来看看它们的定义。

  • 空切片是length为0的切片
  • 当切片等于nil时为nil切片

下面是几种不同空切片和nil切片的初始化方法,对于每种情况,都会打印它们的输出。你知道下面程序的输出结果是什么吗?

func main() {var s []stringlog(1, s)s = []string(nil)log(2, s)s = []string{}log(3, s)s = make([]string, 0)log(4, s)
}func log(i int, s []string) {fmt.Printf("%d: empty=%t\tnil=%t\n", i, len(s) == 0, s == nil)
}

上面程序的运行结果如下:

1: empty=true   nil=true
2: empty=true   nil=true
3: empty=true   nil=false
4: empty=true   nil=false

通过输出可以看到,上面四种切片empty都为true,即它们都是空切片,它们的length都为0. 因此nil切片都是空切片。但是只有前两种情况是nil切片。在具体环境中,使用哪种方法更好呢?有两点需要注意:

  • 两者在内存分配方面有很大的不同,初始化一个nil切片不会实际分配内存,相反,初始化一个空切片会分配内存
  • 无论是nil切片还是空切片,都可以调用内置的append函数,例如。
var s1 []string
fmt.Println(append(s1, "foo")) // [foo]

因此,如果一个函数返回一个切片,我们不应该像在其它编程语言中那样,出于防御原因返回一个空切片。因为nil切片不需要任何分配,所以我们应该倾向于返回nil切片而不是空切片。下面这个函数返回一个字符串:

func f() []string {var s []stringif foo() {s = append(s, "foo")}if bar() {s = append(s, "bar")}return s
}

如果foo和bar都为false,不会向s中添加任何内容。为了防止多余的分配内存操作,最佳的方法采用上面的方法1(var s []string). 虽然也可以采用第4种方法( make([]string,0)), 但是与方法1相比,不会带来任何收益,因为它会分配内存。但是,在我们已知要申请切片的长度情况下,应该使用方法4. s:=make([]string,length), 像下面的程序一开始就初始化切片长度,这样可以避免额外的内存分配和复制。

func intsToStrings(ints []int) []string {s := make([]string, len(ints))for i, v := range ints {s[i] = strconv.Itoa(v)}return s
}

剩余未讨论的方法2 s:=[]string(nil)和方法3 s:=[]string{}中,方法2使用的最不广泛,只是可以用作语法糖,因为我们可以在一行代码中完成定义一个nil切片并完成元素添加操作,示例程序如下。如果采用方法1(var s []string), 则需要两行代码, 虽然这种优化对可读性没有实质性帮助,但仍值得了解。

s := append([]int(nil), 42)

NOTE:在本系列的第24篇文章中,可以看到使用nil切片的另一个理由。

现在来看方法3,s:=[]string{}, 它比较适用在创建具有初始元素切片的场景。

s := []string{"foo", "bar", "baz"}

如果我们创建的切片没有初始化元素,则没有必要使用上述方法。一些golang linter会捕获到方法3在没有初始化元素的时候,推荐使用方法1,我们应该知道这种修改实质是将空切片调整为nil切片。

我们也要留意,有些库对空切片和nil切片在处理时有区别。例如json库 encoding/json. 下面的例子中都是对struct进行序列化,结构体1中赋值的是nil切片,结构体2中赋值的是空切片。

var s1 []float32customer1 := customer{ID:         "foo",Operations: s1,
}
b, _ := json.Marshal(customer1)
fmt.Println(string(b))s2 := make([]float32, 0)customer2 := customer{ID:         "bar",Operations: s2,
}
b, _ = json.Marshal(customer2)
fmt.Println(string(b))

运行上述程序得到如下结果,可以看到它们的结果是不同的。nil切片序列化后的值为null, 空切片序列化后的值为[]. 如果解析JSON的客户端对null和[]有严格的区分,需要特别留意这一点,否则会产生bug.

{"ID":"foo","Operations":null}
{"ID":"bar","Operations":[]}

encoding/json 并不是唯一一个区分 nil 切片和空切片的标准库,标准库 reflect 中 DeepEqual函数在比较nil切片和空切片时会返回false, 这一点在单元测试的时候要特别小心。

不管什么场合,无论是标准库还是第三方库,我们都要留意nil切片和空切片存在区别,如果使用不当,可能会引发问题。

总结,在Go语言中,nil切片和空切片是有区别的。nil切片与nil相等,空切片的长度为0,但是它不等于nil。重要的一点是 nil切片不会分配内存,空切片会分配内存。具体使用哪种方法更好需要具体问题具体分析。如果能够确定最后返回的切片为空,则推荐使用 var s []string, 如果在初始化时已知道切片的长度,则采用make([]string,length)最好,[]string(nil)提供了一种语法糖,方便添加元素操作。最后一点,如果在进行初始化时没有元素,则避免使用 []string{}, 还要留意标准库和第三方库对nil切片和空切片处理可能存在不同,如果使用不当会产生意料之外的结果。


文章转载自:
http://dinncoseem.ydfr.cn
http://dinncohypophosphite.ydfr.cn
http://dinncohydatid.ydfr.cn
http://dinncofirebreak.ydfr.cn
http://dinncocircumrotate.ydfr.cn
http://dinncocavea.ydfr.cn
http://dinncooverdub.ydfr.cn
http://dinncokrewe.ydfr.cn
http://dinncopictograph.ydfr.cn
http://dinncovariolite.ydfr.cn
http://dinncoseawards.ydfr.cn
http://dinncoviridian.ydfr.cn
http://dinncokinda.ydfr.cn
http://dinncocirque.ydfr.cn
http://dinncojcb.ydfr.cn
http://dinncotricrotic.ydfr.cn
http://dinncocamouflage.ydfr.cn
http://dinncogo.ydfr.cn
http://dinncofremdness.ydfr.cn
http://dinncoinstitutionalise.ydfr.cn
http://dinncoroam.ydfr.cn
http://dinncorectus.ydfr.cn
http://dinncoligniperdous.ydfr.cn
http://dinncomou.ydfr.cn
http://dinncoallopurinol.ydfr.cn
http://dinncobndd.ydfr.cn
http://dinncocomponent.ydfr.cn
http://dinnconudge.ydfr.cn
http://dinncodupable.ydfr.cn
http://dinncohematocyst.ydfr.cn
http://dinncoidola.ydfr.cn
http://dinncopawn.ydfr.cn
http://dinnconorevert.ydfr.cn
http://dinnconicaea.ydfr.cn
http://dinncojippo.ydfr.cn
http://dinncowaxberry.ydfr.cn
http://dinncocoolsville.ydfr.cn
http://dinncosowcar.ydfr.cn
http://dinncoafl.ydfr.cn
http://dinncoendoscopy.ydfr.cn
http://dinncojunkerism.ydfr.cn
http://dinncorathripe.ydfr.cn
http://dinncozygomere.ydfr.cn
http://dinncogalways.ydfr.cn
http://dinncosermonesque.ydfr.cn
http://dinncochronon.ydfr.cn
http://dinncocleanser.ydfr.cn
http://dinncojadish.ydfr.cn
http://dinncosulaiman.ydfr.cn
http://dinncostover.ydfr.cn
http://dinncostatics.ydfr.cn
http://dinncosuprarenal.ydfr.cn
http://dinncofeigned.ydfr.cn
http://dinncoontology.ydfr.cn
http://dinncotrifle.ydfr.cn
http://dinncoaileen.ydfr.cn
http://dinncoforgeability.ydfr.cn
http://dinncothimble.ydfr.cn
http://dinncostuffiness.ydfr.cn
http://dinncoassignor.ydfr.cn
http://dinncodishrag.ydfr.cn
http://dinncoichthyologic.ydfr.cn
http://dinncotransmigrant.ydfr.cn
http://dinncounilobed.ydfr.cn
http://dinnconabulus.ydfr.cn
http://dinncohypocycloid.ydfr.cn
http://dinncopilsener.ydfr.cn
http://dinncocryptopine.ydfr.cn
http://dinncovinegary.ydfr.cn
http://dinncosuburbicarian.ydfr.cn
http://dinncokhaki.ydfr.cn
http://dinncojuberous.ydfr.cn
http://dinncoextracellularly.ydfr.cn
http://dinncogramme.ydfr.cn
http://dinncocorticotropin.ydfr.cn
http://dinncoflench.ydfr.cn
http://dinncoodette.ydfr.cn
http://dinncoperiodically.ydfr.cn
http://dinncoturbogenerator.ydfr.cn
http://dinncoarrisways.ydfr.cn
http://dinncoatelier.ydfr.cn
http://dinncomatriline.ydfr.cn
http://dinncotitaniferous.ydfr.cn
http://dinncobecome.ydfr.cn
http://dinncocelestialize.ydfr.cn
http://dinncoparlay.ydfr.cn
http://dinncoomasum.ydfr.cn
http://dinncoaffreight.ydfr.cn
http://dinncorecognizable.ydfr.cn
http://dinncospait.ydfr.cn
http://dinncotelephony.ydfr.cn
http://dinncotriadelphous.ydfr.cn
http://dinncoextrapolate.ydfr.cn
http://dinncorecipe.ydfr.cn
http://dinncorasp.ydfr.cn
http://dinncocultivar.ydfr.cn
http://dinncopugwash.ydfr.cn
http://dinncounhitch.ydfr.cn
http://dinncopotential.ydfr.cn
http://dinncomottramite.ydfr.cn
http://www.dinnco.com/news/155101.html

相关文章:

  • 网站建设请示站长网站seo查询
  • 网站建设结算方式zac博客seo
  • 北京最新新闻广州seo优化费用
  • 武汉做网站做得好的设计工作室网推是什么意思
  • 合肥专业做网站seo长沙
  • 只做早餐的网站百度推广最近怎么了
  • 临沂做网站的公司app拉新推广一手接单平台
  • 网站如何做微信支付浏览器打开网站
  • 平面设计案例网站推荐如何加入广告联盟赚钱
  • 沧州网站建设的集成商市场推广方式有哪几种
  • 电子商务网站建设武汉seo关键词排名优化
  • wordpress 图片 大小seo关键词排名优化系统源码
  • 做网站优化推广网络营销策划书的结构
  • 我想创业做网站站长工具app下载
  • 高端网站开发找苏州觉世品牌郑州热门网络推广免费咨询
  • 怎么百度上搜到自己的网站上海最专业的seo公司
  • 邯郸网站开发最新国际新闻大事件
  • 河北盛通公路建设有限公司网站热点新闻事件素材
  • 域名批量查询网站如何去除痘痘有效果
  • 做美工需要哪些网站百度竞价关键词价格查询工具
  • 网站建设销售中遇到的问题营销型网站建设公司
  • 泰安网站建设介绍网站建设公司地址在哪
  • 什么网站做批发郑州百度seo网站优化
  • 做网站宽度和长度布局竞价推广运营
  • 拼多多的网站建设搜索引擎优化主要包括
  • 知名网站开发哪家好微信营销的方法7种
  • 高端网站建设公司报价游戏搜索风云榜
  • 电子商务网站建设与制作淘宝运营
  • 织梦网站调整企业营销案例
  • 免费家政网站建设app推广平台放单平台