How to expand array values in string list

I have following code as shown below

a[0] = "Hello"
a[1] = "World"
a[2] = "something" = []string{"a[0]", "Cool stuff"}

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

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" = append(a, "Cool Stuff")

@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

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.


@Sandertv Excellent it worked

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.