Combining strings
The Go standard library offers multiple ways to build strings from components. The best way depends on what type of strings you are dealing with, and how long they are. This section shows several ways that strings can be built.
How to do it...
- To combine a few fixed numbers of strings, or to add runes to another string, use the
+
or+=
operators orstring.Builder
- To build a string algorithmically, use
strings.Builder
- To combine a slice of strings, use
strings.Join
- To combine parts of URL paths, use
path.Join
- To build filesystem paths from path segments, use
filepath.Join
How it works...
To build constant values, or for simple concatenations, use the +
or +=
operators:
var TwoLines = "This is the first line \n"+ "This is the second line" func ThreeLines(newLine string) string { return TwoLines+"\n"+newLine }
You can add runes to a string the same way:
func AddNewLine(line string...