Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently concatenate string array and string in golang

Tags:

go

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?

like image 238
Rahul Avatar asked Dec 01 '13 07:12

Rahul


People also ask

How to concatenate strings in Go (aka Golang)?

To concatenate strings in Go (aka Golang) we can use strings.Builder and bytes.Buffer types. In Go language, string is a primitive type and it is immutable. Whenever we change a string or append to another string, we will be creating a new string.

How to concatenate strings in Python?

The strings package has a builder type which is a very efficient way of building strings. It uses much less memory when concatenating strings and is a better way of concatenation. The function WriteString allows us to concatenate strings in a faster way. Below is an example of string concatenation using the strings builder method.

How to compare strings in Golang?

Comparing Strings in GoLang 1 Using the GoLang Comparison Operators#N#There is a multitude of comparison operators in Go. Each operator works in the... 2 GoLang Strings Compare () method#N#The strings package contains a compare method. The signature of this method is shown... 3 Strings EqualFold () method to compare strings More ...

How to concatenate strings with builder type in go?

The Builder type is added in Go 1.10 version as part of strings package. A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. We can use WriteString function of Builder type to concatenate strings.


2 Answers

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

like image 150
Vitruvie Avatar answered Oct 14 '22 07:10

Vitruvie


Bill DeRose and Saposhiente are correct about how slices work.

As for a simpler way of solving your problem, you could also do (play):

fmt.Println("join is",strings.Join(append(s[:1],sfinal...)," "))
like image 25
David Budworth Avatar answered Oct 14 '22 08:10

David Budworth