用+替换字符串中的所有空格

I have a string and I want to replace every space in this string with a + I tired this by using:

tw.Text = strings.Replace(tw.Text, " ", "+", 1)

But that didn't worked for me...any solutions?

For example the string could look like:

The answer of the universe is 42

from the Go documentation: func Replace

If n < 0, there is no limit on the number of replacements.

try

strings.Replace(tw.Text, " ", "+", -1)

Documentation on strings.Replace(): http://golang.org/pkg/strings/#Replace

According to the documentation, the fourth integer parameter is the number of replacements. Your example would only replace the first space with a "+". You need to use a number less than 0 for it to not impose a limit:

tw.Text = strings.Replace(tw.Text, " ", "+", -1)

If you are using this in a query, the QueryEscape method provided by net/url is the best solution: https://golang.org/pkg/net/url/#QueryEscape

import "net/url"

tw.Text = url.QueryEscape(tw.Text)