Go map of functions

Are you trying to do something like this? I’ve revised the example to use varying types and numbers of function parameters. package main import “fmt” func f(p string) { fmt.Println(“function f parameter:”, p) } func g(p string, q int) { fmt.Println(“function g parameters:”, p, q) } func main() { m := map[string]interface{}{ “f”: f, “g”: … Read more

Map with concurrent access

Multiple readers, no writers is okay: https://groups.google.com/d/msg/golang-nuts/HpLWnGTp-n8/hyUYmnWJqiQJ One writer, no readers is okay. (Maps wouldn’t be much good otherwise.) Otherwise, if there is at least one writer and at least one more either writer or reader, then all readers and writers must use synchronization to access the map. A mutex works fine for this.

Iterating through a golang map

For example, package main import “fmt” func main() { type Map1 map[string]interface{} type Map2 map[string]int m := Map1{“foo”: Map2{“first”: 1}, “boo”: Map2{“second”: 2}} //m = map[foo:map[first: 1] boo: map[second: 2]] fmt.Println(“m:”, m) for k, v := range m { fmt.Println(“k:”, k, “v:”, v) } } Output: m: map[boo:map[second:2] foo:map[first:1]] k: boo v: map[second:2] k: foo … Read more

How to count items in a Go map?

Use len(m). From http://golang.org/ref/spec#Length_and_capacity len(s) string type string length in bytes [n]T, *[n]T array length (== n) []T slice length map[K]T map length (number of defined keys) chan T number of elements queued in channel buffer Here are a couple examples ported from the now-retired SO documentation: m := map[string]int{} len(m) // 0 m[“foo”] = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)