Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Idiomatic vs non-idiomatic
package main
import "fmt"
type myMap map[string]string
func (m myMap) contains(key string) (val string, ok bool) {
val, ok = m[key]
return val, ok
}
func (m myMap) add(key, value string) {
m[key] = value
}
func main() {
// non idiomatic
var m = make(map[string]string)
m["x"] = "hi"
if v, ok := m["x"]; ok {
fmt.Println(v)
} else {
fmt.Println("not found")
}
// idiomatic
var idiomaticM = make(myMap)
idiomaticM.add("x", "hi")
if v, ok := idiomaticM.contains("x"); ok {
fmt.Println(v)
} else {
fmt.Println("not found")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.