How to dynamically insert value in a slice

8,406 views
Skip to first unread message

Jai Kumar

unread,
Sep 20, 2012, 5:52:23 PM9/20/12
to golan...@googlegroups.com
package main
import "fmt"

var fname = make([]string, 10)

func main() {
        fname[0] = "bar"
        fname[1] = "rok"
        
        name := []string{"john", "ton", "bon", "foo"}
for i := 0; i<len(name); i++ {
  //How to dynamically insert value in to slice fname. Suppose I dont knwo how many elemnts are already present in fname slice already
           //here fname[numbers of elements already there + 1] should start. 
  fname[] = name[i]   // <--THIS FAILS HERE <--
}
for i := 0; i<len(fname); i++ {
  fmt.Println(fname[i])
}
}

How to insert value dynamically in a slice? Please dont assume that we know how many values (it will be alwasy less than 10 though) are already stored in the fname slice already.

Francisco Souza

unread,
Sep 20, 2012, 6:03:51 PM9/20/12
to Jai Kumar, golan...@googlegroups.com
You can use append: http://golang.org/pkg/builtin/#append

--
~f

Kyle Lemons

unread,
Sep 20, 2012, 6:05:21 PM9/20/12
to Francisco Souza, Jai Kumar, golan...@googlegroups.com

Jai Kumar

unread,
Sep 20, 2012, 6:14:19 PM9/20/12
to golan...@googlegroups.com, Francisco Souza, Jai Kumar
Thanks Farncisco and Kyle. Both works well. 

Kyle, your approach is much generic and work well in all conditions (good), but due to my specific need, I modified my code and come up with http://play.golang.org/p/HA2OCAVDkb 

Uriel

unread,
Sep 20, 2012, 6:36:51 PM9/20/12
to Jai Kumar, golan...@googlegroups.com, Francisco Souza
On Fri, Sep 21, 2012 at 12:14 AM, Jai Kumar <jaiva...@gmail.com> wrote:
> Thanks Farncisco and Kyle. Both works well.
>
> Kyle, your approach is much generic and work well in all conditions (good),
> but due to my specific need, I modified my code and come up with
> http://play.golang.org/p/HA2OCAVDkb

This does exactly the same, but is much more idiomatic code:

http://play.golang.org/p/meA91ACA1T


Also this is a must-read:

http://code.google.com/p/go-wiki/wiki/SliceTricks


Uriel

Kyle Lemons

unread,
Sep 20, 2012, 7:44:24 PM9/20/12
to Jai Kumar, golan...@googlegroups.com, Francisco Souza
On Thu, Sep 20, 2012 at 3:14 PM, Jai Kumar <jaiva...@gmail.com> wrote:
Thanks Farncisco and Kyle. Both works well. 

Kyle, your approach is much generic and work well in all conditions (good), but due to my specific need, I modified my code and come up with http://play.golang.org/p/HA2OCAVDkb 

I pulled the data that was going into the slice into the gen goroutine so that it wouldn't be possible to cheat.  You specifically said you don't know how many you had beforehand, so I embodied that in my example.  The append was the main point of my code, not the goroutine or the channel.
 
On Thursday, September 20, 2012 3:05:53 PM UTC-7, Kyle Lemons wrote:
On Thu, Sep 20, 2012 at 3:03 PM, Francisco Souza <f...@souza.cc> wrote:

On Thu, Sep 20, 2012 at 6:52 PM, Jai Kumar <jaiva...@gmail.com> wrote:
> package main
> import "fmt"
>
> var fname = make([]string, 10)
>
> func main() {
>         fname[0] = "bar"
>         fname[1] = "rok"
>
>         name := []string{"john", "ton", "bon", "foo"}
> for i := 0; i<len(name); i++ {
>   //How to dynamically insert value in to slice fname. Suppose I dont knwo
> how many elemnts are already present in fname slice already
>            //here fname[numbers of elements already there + 1] should start.
>   fname[] = name[i]   // <--THIS FAILS HERE <--
> }
> for i := 0; i<len(fname); i++ {
>   fmt.Println(fname[i])
> }
> }
>
> How to insert value dynamically in a slice? Please dont assume that we know
> how many values (it will be alwasy less than 10 though) are already stored
> in the fname slice already.

You can use append: http://golang.org/pkg/builtin/#append

--
 
 

Reply all
Reply to author
Forward
0 new messages