Member-only story

How to Use Golang Structs With MongoDB

In this short example, I’ll show you how to create structs in the Go Language for use with MongoDB

Jes Fink-Jensen
Better Programming
6 min readApr 12, 2022

--

Photo by Ana Abad on Unsplash

To demonstrate the use of Golang structs with MongoDB, I have created a simple email autoresponder as an example. We have some contacts to whom we wish to send emails. In addition, we have emails to send at specific times, with subject and content. Lastly, we also have a sequence that connects the contacts to the emails.

By using Golang structs, we minimize the use of BSON in our code and increase the usability of the results from the database queries. This article can be seen as a continuation of a previous article called “How To Use Go With MongoDB.”

In what follows, I go over the code of a little demo, piece by piece.

Importing the relevant packages

To start, we’ll import the necessary Golang packages.

We need to import these libraries to do the following:

  • context is necessary to…

--

--

No responses yet

What are your thoughts?