dongxia2068 2010-10-18 15:29
浏览 18
已采纳

首选的声明/初始化方法

I'm currently playing with Google Go.
There's a lot of ways to declare and/or initialize variables.
Can someone explains pros/cons of each way (samples, as far as i know, below) :

    var strArr0 *[10]string = new([10]string)

    var strArr1 = new([10]string)

    var strArr2 = make([]string,10)

    var strArr3 [10]string

    strArr4 := make([]string,10)

What is your preferred syntax, and why ?
Thanks, SO folks !

  • 写回答

2条回答 默认 最新

  • dongliang9682 2010-11-01 21:05
    关注

    I've numbered your examples 1-5, and I'll walk through them here. Hope this helps!

    var strArr0 *[10]string = new([10]string)  // (1)
    

    This allocates a new array of strings, of length 10, and returns a pointer to the array.

    var strArr1 = new([10]string)  // (2)
    

    This is exactly the same as 1. It's just shorthand, that works due to Go's type inference. It could be shortened further to:

    strArr1 := new([10]string)  // (2a)
    

    Where 1, 2, 2a all produce exactly the same result.

    var strArr2 = make([]string,10)  // (3)
    

    This makes a slice of strings, with length 10. A slice refers to some subset of an underlying array. From the golang spec:

    The make() call allocates a new, hidden array to which the returned slice value refers.

    make([]T, length, capacity)
    

    produces the same slice as allocating an array and slicing it, so these two examples result in the same slice:

    make([]int, 50, 100)
    new([100]int)[0:50]
    

    So 3 is equivalent to any of the following:

    var strArr2 = new([10]string)[0:10]  // Slicing an explicitly created array
    var strArr2 []string = new([10]string)[0:10]  // Explicitly declaring the type of strArr2, rather than inferring
    strArr2 := new([10]string)[0:10]  // Using the := shorthand instead of var
    

    new or make are used depending on what type you're creating. make is used solely for slices, maps and channels. They used a different keyword to convey the idea that make is initializing some data structure under the hood, rather than just zeroing memory.

    The next one is back to arrays rather than slices:

    var strArr3 [10]string  // (4)
    

    This is the same as 1, 2, and 2a.

    strArr4 := make([]string,10)  // (5)
    

    Same as 3. := is just shorthand when initializing a variable and the type can be inferred.


    So which to choose? This is up to your personal style a bit, but in general one choice is obviously going to maximise the clarity of your code e.g. using type inference when the type is obvious:

    foo := bar.ToStringArray()
    

    or declaring the types when it's less so and the type will be useful to see:

    var foo []string = bar.DoSomethingOpaque()
    

    On slices versus arrays, you'll usually create whatever type is required for some function you're calling.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)