How to efficiently concatenate string array and string in golang

go

Solution

The whitespace is due to all of the empty elements in dep_string. When you use the make function, it creates a slice with the specified length and capacity, filled with a bunch of nothing. Then, when you use append, it sees that the slice has reached its maximum capacity, extends the slice, then adds your elements, after all of the nothing. The solution is to make a slice with the capacity to hold all of your elements, but with initial length zero:

dep_string := make ([]string, 0, len(tests) + len(sfinal))

strings.TrimSpace is unnecessary. You can read more at http://blog.golang.org/slices

Problem

I have a bunch of strings and []strings in golang which I need to concatenate. But for some reason I am getting a lot of whitespaces along the way which I need to get rid of. Here's the code ``` tests := strings.TrimSpace(s[0]) dep_string := make ([]string, len(tests) + len(sfinal)) dep_string = append (dep_string,tests) for _,v := range sfinal { dep_string = append(dep_string,v) } fmt.Println("dep_String is ",dep_string) Input: s[0] = "filename" sfinal = [test test1] expected output [filename test test1] actual output [ filename test test1] ``` It's really weird; even after using TrimSpace I am not able to get rid of excess space. Is there any efficient way to concatenate them?

Original source