Recently I’ve been working on a new personal project called Banter Bus, a browser-based multiplayer game. I’ve been working on a REST API to add new questions to the game. The API is built in Golang and uses MongoDB as the database. Since Golang is a strongly typed language, we will need to specify the structure of the data we expect from the database. This can get tricky if the data varies, such as one field changing.

One issue I encountered was each game type has to have its questions. These questions will be asked to the users playing the game and are stored differently in the database. This is because each game type has different rules and therefore needs a different structure. This means when we unmarshal the data in Golang, we need to specify the structure of these questions. In this article, I will explain how you can create your own unmarshal function. This will allow you to customise the struct that will hold this data (in Golang) returned from MongoDB.

#mongodb #golang #database #programming #developer

Golang and MongoDB with Polymorphism and BSON Unmarshall
5.25 GEEK