• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Why is this type conversion necessary?

Resolved: Why is this type conversion necessary?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have the following code, making use of go.mongodb.org/mongo-driver libraries:
If I do:
it doesn’t compile. The compiler already knows that updateMap is of type bson.M which is a map[string]interface{}. How does asserting that it really is a map[string]interface{} doing anything useful for the compiler? I thought it would want to know what kind of interface it actually is, but apparently it doesn’t want me to do that.
What is going on here?
edit: and of course, it should be noted that without a type assertion here, it doesn’t compile:

invalid operation: cannot index updateMap["$addFields"] (map index expression of type interface{})


Answer:

bson.M is defined as an alias of primitive.M, which is defined as type M map[string]interface{} (that is, a non-alias type). The problem is that updateMap["$set"] is a bson.M, which is a distinct type compared to map[string]interface{}. So your type assertion would have to be to bson.M instead, for example:
Runnable example in Go playground.

If you have better answer, please add a comment about this, thank you!

go
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.