FAQ
Hi! I'm going through tour.golang.org
I'm on #34. It mentions you can specify the capacity in addition to the
length when making a new slice.

What are the difference between the two? I couldn't find the answer
anywhere.

package main

import "fmt"

func main() {

     a := make([]int, 5)
     a[0]=1
     printSlice("a", a)
     b := make([]int, 0, 5)

     printSlice("b", b)
     c := b[:2]
     printSlice("c", c)
     d := c[2:5]
     printSlice("d", d)

}

func printSlice(s string, x []int) {
     fmt.Printf("%s len=%d cap=%d %v\n",
         s, len(x), cap(x), x)
}


a len=5 cap=5 [1 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0]
d len=3 cap=3 [0 0 0]

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • David Symonds at Sep 5, 2014 at 1:49 am
    Read http://blog.golang.org/go-slices-usage-and-internals. It'll
    answer all your questions.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ibrahim M. Ghazal at Sep 5, 2014 at 3:47 am

    On Fri, Sep 5, 2014 at 4:49 AM, David Symonds wrote:
    Read http://blog.golang.org/go-slices-usage-and-internals. It'll
    answer all your questions.
    This blog post was written before the s[a:b:c] syntax. Any chance of
    updating it to include it?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Sep 5, 2014 at 4:13 am
    http://blog.golang.org/slices is the one he meant to name. It says
    everything you need to know.

    If you want to use three-index slicing, you will notice it's not
    described there. That is deliberate; it is for experts only.

    -rob

    On Thu, Sep 4, 2014 at 8:46 PM, Ibrahim M. Ghazal wrote:
    On Fri, Sep 5, 2014 at 4:49 AM, David Symonds wrote:
    Read http://blog.golang.org/go-slices-usage-and-internals. It'll
    answer all your questions.
    This blog post was written before the s[a:b:c] syntax. Any chance of
    updating it to include it?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Akwillis at Sep 9, 2014 at 2:56 am
    That is like teaching the english language without adjectives or
    determiners...

    If you want to use three-index slicing, you will notice it's not
    described there. That is deliberate; it is for experts only.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 5, '14 at 1:48a
activeSep 9, '14 at 2:56a
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase