if _, ok := map[thing]; !ok {
fmt.Println("not in map")
}
// or with assignment
if foo, ok := map[thing]; !ok {
fmt.Println("not in map")
} else {
fmt.Printf("Thing: %+v", foo")
}
The difference is just error checking essentially, which is idiomatic with go. Either way neither of these I feel like have anything to do with the objective value of the typing systems or design, it's how both (or at least Go, I'm not too familiar with Rust) languages were designed.
The problem is that nothing prevents you from accessing foo when item does not exist in the map: https://play.golang.org/p/SrZ1Pn5g8uR. This is something solved by both exceptions and Result types. And it is a type system thing, because the ability to have tagged unions and do pattern matching on them is part of of the type system.
The compiler isn’t going to stop you from writing unsafe code. If you access a that value before checking the error in the assignment expression that’s not a fault of the typing system or the language.
4
u/asdfkjasdhkasd Jun 28 '18
_, ok := map[thing]is really not that good if you have seen some of the better alternatives.Look at this rust: