How to expand array values in string list


(SANJAY SHITOLE) #1

Hello,
I have following code as shown below

a[0] = "Hello"
a[1] = "World"
a[2] = "something"

vs.top = []string{"a[0]", "Cool stuff"}

I want to grow vs.top = []string{“Hello”, “World”, “Something”, “Cool stuff”}
so basically I get the array updated by reading some data and I would like to update vs.top
Thanks


(Sandertv) #2

You can simply append it to the slice to return a new slice with the value added:

a[0] = "Hello"
a[1] = "World"
a[2] = "something"

vs.top = append(a, "Cool Stuff")

(SANJAY SHITOLE) #3

@Sandertv thanks, I would like to use with the string, if I use it then it gives me error

vs.Rules = []string{append(a, "Cool stuff")}
go run f5gohello.go
# command-line-arguments
./f5gohello.go:184:32: cannot use append(a, "Cool stuff") (type []string) as type string in array or slice literal

(Sandertv) #4

If you’d like to join them as a single string, you’ll have to use strings.Join(append(a, "Cool stuff"), " ") to join the strings with a space between the values.


(SANJAY SHITOLE) #5

@Sandertv Excellent it worked

vs.Rules = []string{strings.Join(append(a, "Cool stuff"), "")}