About
Text - String in Go
Management
Declaration
The following declarations are all equivalent:
// The short variable declaration (only within a function, not for package-level variables)
s := ""
// Initialized to the default ""
var s string
var s = ""
var s string = ""
Concatenation
For:
- a few. The += statement. It makes a new string by concatenating the old string, a space character, and the next argument, then assigns the new string to s. The old contents of s are no longer in use, so they will be garbage-collected in due course. If the amount of data involved is large, this could be costly
- a big string construction. Use the Join function from the strings package.
Scanner
Scanner that reads input and breaks it into lines or words;
Example: get a count of text on a line. All duplicate have more than one counts.
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
}