C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We append to the string slice at the key "dog." The word "dog" is not important—you can change it.
New slice: We add a new slice at the key "fish" with some more color words: "orange" and "red."
Range: Finally we use a for-range loop over the slice at the key "fish." We print indexes and values for the string slice.
ForGolang program that uses map of string slices
package main
import "fmt"
func main() {
// Create map of string slices.
m := map[string][]string{
"cat": {"orange", "grey"},
"dog": {"black"},
}
// Add a string at the dog key.
// ... Append returns the new string slice.
res := append(m["dog"], "brown")
fmt.Println(res)
// Add a key for fish.
m["fish"] = []string{"orange", "red"}
// Print slice at key.
fmt.Println(m["fish"])
// Loop over string slice at key.
for i := range m["fish"] {
fmt.Println(i, m["fish"][i])
}
}
Output
[black brown]
[orange red]
0 orange
1 red
Tip: In the Markov chain algorithm, each prefix is used as a key to a slice of suffixes (a string slice).
Quote: A chain consists of a prefix and a suffix. Each prefix is a set number of words, while a suffix is a single word. A prefix can have an arbitrary number of suffixes.
Generating arbitrary text: golang.org