TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Map With String Slice Values

Use a map with string keys and string slices as the values. Add data to this map.
Map, string slices. In Go programming we reuse common forms. We have string slices—these can contain many strings, and we can append strings as needed.map
With a map, we can place string slices as the values. So we can associate string keys with lists of values. This is powerful. It helps in many Go programs.
Main example. Here we use a map of string slices in the main func. We initialize the map with two keys—each has its own string slice.

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.

For
Golang 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
Some research, Markov. On the Golang site we find an example implementation of a Markov chain. A core data structure for this is a map of string slices.

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
With append, we can add elements to a string slice in a map. And with delete we can remove elements. Len will return the element count.SliceSlice: len
In Go, common things like a map that contains string slices are important to master. More complex things like chans are nice, but often used less.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf