Configuring Go's MongoDB driver
The Recipes API we implemented in the previous chapter is written in Golang. Therefore, we need to install the official MongoDB Go driver (https://github.com/mongodb/mongo-go-driver) to interact with the MongoDB server. The driver fully integrates with the MongoDB API and supports all the main queries and aggregation features of the API.
Issue the following command to install the package from GitHub:
go get go.mongodb.org/mongo-driver/mongo
This will add the package as a dependency in the require
section, under the go.mod
file:
module github.com/mlabouardy/recipes-api go 1.15 require ( Â Â Â github.com/gin-gonic/gin v1.6.3 Â Â Â github.com/rs/xid v1.2.1 Â Â Â go.mongodb.org/mongo-driver v1.4.5 )
To get started, import the following packages in the main.go
file:
package main import ( Â Â Â "go.mongodb.org/mongo-driver/mongo" Â Â Â "go.mongodb.org/mongo...