Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate empty string and nothing in a map [duplicate]

Tags:

go

The following code yields true. So I'm wondering for map[string]string in Golang, is there a way to differentiate empty string and nothing?

package main

import "fmt"

func main() {
    m := make(map[string]string)
    m["abc"] = ""
    fmt.Println(m["a"] == m["abc"]) //true
}

1 Answers

If by "nothing" you mean that the element is not in the map you can use the ok idiom:

val, ok := myMap["value"] // ok is true if value was in the map

You can find more information in Effective Go.

like image 96
Stephan Dollberg Avatar answered Nov 30 '25 11:11

Stephan Dollberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!