<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kevin Wafula</title>
    <description>The latest articles on DEV Community by Kevin Wafula (@wafulakevin).</description>
    <link>https://dev.to/wafulakevin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1064441%2F1e78bbfc-2132-47fa-9955-5980f052d3c6.jpg</url>
      <title>DEV Community: Kevin Wafula</title>
      <link>https://dev.to/wafulakevin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wafulakevin"/>
    <language>en</language>
    <item>
      <title>Gin Tutorial: Developing a RESTful API using GIN</title>
      <dc:creator>Kevin Wafula</dc:creator>
      <pubDate>Mon, 18 Sep 2023 09:39:26 +0000</pubDate>
      <link>https://dev.to/wafulakevin/gin-tutorial-developing-a-restful-api-using-gin-4mik</link>
      <guid>https://dev.to/wafulakevin/gin-tutorial-developing-a-restful-api-using-gin-4mik</guid>
      <description>&lt;p&gt;Gin is a web framework written in Golang. It has high performance and good productivity. In this article, we’ll be building a crud API using Gin. This will provide a deeper understanding of the web framework. &lt;br&gt;
The main topic include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create folder for your project&lt;/li&gt;
&lt;li&gt; Create Packages for your code&lt;/li&gt;
&lt;li&gt; Create the Data structures to handle data (models).&lt;/li&gt;
&lt;li&gt; Setting up handler functions using gin &lt;/li&gt;
&lt;li&gt; Use of the MongoDB Go Driver to connect with the MongoDB database&lt;/li&gt;
&lt;li&gt; API endpoints and Routing&lt;/li&gt;
&lt;li&gt; Testing using Postman&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pre-Requisites
&lt;/h2&gt;

&lt;p&gt;• Working knowledge of Golang&lt;br&gt;
• Have Go installed&lt;br&gt;
• Some experience working with the MongoDB Go driver. If not please check out this article on the same.&lt;br&gt;
• A text editor i.e. Visual Studio&lt;br&gt;
• Postman or a curl tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Create folder for your project
&lt;/h2&gt;

&lt;p&gt;Let’s create a project for the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dKuInA0h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjfxotnpaz6ojvwf7e8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dKuInA0h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjfxotnpaz6ojvwf7e8o.png" alt="Image description" width="800" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;• Go to the directory where you store your Go projects&lt;br&gt;
• Create a new directory&lt;br&gt;
• Finally create a module for managing your dependencies by running &lt;code&gt;go mod init&lt;/code&gt; and include the path in which your module will exist. In this case &lt;code&gt;github.com/kevin.&lt;/code&gt;&lt;br&gt;
• Next open the project in your editor. For vs code just type code .&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Create Packages for your code
&lt;/h2&gt;

&lt;p&gt;Golang codes are organized into packages. The project structure should adhere to the &lt;a href="https://go.dev/doc/tutorial/create-module#:~:text=Go%20code%20is%20grouped%20into,new%20versions%20of%20the%20module."&gt;go conventions.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lKeSwFcb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5jgwg38w779a0uhnkfw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lKeSwFcb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5jgwg38w779a0uhnkfw.png" alt="Image description" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Controllers&lt;/strong&gt;
• The controllers package contains the logic for handling HTTP requests and managing the application's business logic.
• Controllers are responsible for parsing incoming HTTP requests, processing data, interacting with models (if necessary), and sending HTTP responses.
• Controllers act as intermediaries between the incoming HTTP requests (handled by the router) and the application's data models.
• Example functions in a controller might include handling user registration, login, data retrieval, or any other application-specific logic related to a route.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Database&lt;/strong&gt;
The database package typically contains code responsible for interacting with the application's database system.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Models&lt;/strong&gt;
• The models package contains data structures and code related to the application's data domain.
• It defines the data structures that represent the core objects or entities in your application, such as User, Product, Order, etc.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Routes&lt;/strong&gt;
• The routes package is responsible for defining the application's URL routes and configuring the routing behavior.
• It often contains code for setting up the router (e.g., using a package like Gorilla Mux) and mapping routes to specific controller functions.
This is just a basic implementation of the MVC pattern to ensure maintainability and separation of concerns. For advanced projects you could include the middleware packages etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3.   Create the Data structures to handle data (models).
&lt;/h2&gt;

&lt;p&gt;Any API should interact with a database. In this case we’ll implement the MongoDB using the MongoDB Go Driver&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; In models.go import the &lt;code&gt;go.mongodb.org/mongo-driver/bson/primitive&lt;/code&gt; package in the MongoDB Go driver provides support for &lt;strong&gt;BSON (Binary JSON)&lt;/strong&gt; primitive types. BSON is the binary serialization format used by MongoDB to store and exchange data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MmueH9u8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3klgzctip8m8lp1hj65q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MmueH9u8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3klgzctip8m8lp1hj65q.png" alt="Image description" width="676" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Below the import statement declare an album &lt;strong&gt;struct&lt;/strong&gt;. Should include the id, artist, title of album and the price.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X3gCZzNb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64ytckw7khaxgr89azy8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X3gCZzNb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64ytckw7khaxgr89azy8.png" alt="Image description" width="661" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Setting up handler functions with Gin
&lt;/h2&gt;

&lt;p&gt;Write a handler function to return all the items.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; In the controllers.go file import the necessary packages.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WJKSEz4x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2hkxpfobxig5pjbzvws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WJKSEz4x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2hkxpfobxig5pjbzvws.png" alt="Image description" width="688" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Declare a variable &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xBl06wjj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njggigikki26trw9j079.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xBl06wjj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njggigikki26trw9j079.png" alt="Image description" width="800" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve declared a variable called &lt;em&gt;AlbCollection&lt;/em&gt; that points to the mongo.Collection as its type.&lt;br&gt;
This variable will hold a reference to a MongoDB collection, which is a place where you can store and query documents (data) in MongoDB. albums is the name of the mongo DB collection.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Next, copy this function below the above.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CKNyika---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3h9m90u3vhe8zinyg25.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CKNyika---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3h9m90u3vhe8zinyg25.png" alt="Image description" width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, you:&lt;br&gt;
• Write a function called &lt;em&gt;GetAlbums&lt;/em&gt; that takes *&lt;em&gt;gin.Context&lt;/em&gt; as its parameter.&lt;br&gt;
• Create a context with a timeout of 100 seconds&lt;br&gt;
• Use the AlbCollection to find all documents in the collection&lt;br&gt;
• Create a slice to hold the albums retrieved from the database and handle any errors&lt;br&gt;
• Loop through the cursor to decode and collect each album and append the decoded album to the albums slice&lt;br&gt;
• Finally, return the list of albums as a JSON response with an HTTP status of 200 (OK)&lt;/p&gt;

&lt;h2&gt;
  
  
  Write a function to post items into the database
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzqLXeb4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv2dx7zfc46a5gwyx3dq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzqLXeb4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv2dx7zfc46a5gwyx3dq.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, you: &lt;br&gt;
• Write a handler function to handle the creation of a new album resource in a web application using the Gin web framework and MongoDB for data storage.&lt;br&gt;
• In the function, you create a &lt;strong&gt;newAlbum&lt;/strong&gt; variable of type models. Album and bind the incoming JSON request body to the &lt;strong&gt;newAlbum&lt;/strong&gt; variable&lt;br&gt;
• Next, you generate a new ObjectID for the album&lt;br&gt;
• Finally, insert the &lt;strong&gt;newAlbum&lt;/strong&gt; into the **AlbCollection **and handle the error if any.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write a function to get a single item by ID
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jPkmxMAt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ha1nre1px6atduvi2o2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jPkmxMAt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ha1nre1px6atduvi2o2.png" alt="Image description" width="800" height="547"&gt;&lt;/a&gt;&lt;br&gt;
In this code, you:&lt;br&gt;
• Write the &lt;em&gt;GetAlbumByID&lt;/em&gt; function that is designed to retrieve an album from a MongoDB collection by its unique identifier (ID) and return it as a JSON response.&lt;br&gt;
• In the function, you first retrieve the albumID from the URL parameter and then you create a context with a timeout of 100 seconds&lt;br&gt;
• Next, parse the albumID into an ObjectID and handle any arising error.&lt;br&gt;
• Then declare a variable to store the album data and query the AlbCollection to find the album with the matching ObjectID. Handle the errors if any.&lt;br&gt;
• Finally, return the found album as a JSON response with an HTTP status of 200 (OK)&lt;/p&gt;

&lt;h2&gt;
  
  
  Write a function to modify an album
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JnU43DBd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4mxfcbc6s34zkd0e1j4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JnU43DBd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4mxfcbc6s34zkd0e1j4.png" alt="Image description" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This function is designed to update an existing album in a MongoDB collection based on the provided album ID and the new album data provided in the JSON request body.&lt;br&gt;
• First, you retrieve the albumID from the URL parameter and then you create a context with a timeout of 100 seconds&lt;br&gt;
• Then, you declare a variable &lt;strong&gt;updatedAlbum&lt;/strong&gt; to store the updated album data &lt;br&gt;
• Bind the incoming JSON request body to the &lt;strong&gt;updatedAlbum&lt;/strong&gt; variable and handle the errors if any.&lt;br&gt;
• Parse the &lt;strong&gt;albumID&lt;/strong&gt; into an ObjectID and handle the errors&lt;br&gt;
• Define a filter to find the album with the specified ObjectID&lt;br&gt;
• Define an update with the new album data using the &lt;code&gt;"$set"&lt;/code&gt;operator&lt;br&gt;
• Update the album in the AlbCollection and handle any errors related to writing documents in the collection.&lt;br&gt;
• Return a JSON response indicating that the album has been updated&lt;/p&gt;

&lt;h2&gt;
  
  
  Write a function to delete an album
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6vOgVtrX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjnqywb44ecwnbt6bafr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6vOgVtrX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjnqywb44ecwnbt6bafr.png" alt="Image description" width="800" height="524"&gt;&lt;/a&gt;&lt;br&gt;
This function deletes an existing album from a MongoDB collection based on the provided album ID. It's likely used as a handler for an HTTP DELETE request to remove an album resource.&lt;br&gt;
• First, you retrieve the albumID from the URL parameter and then you create a context with a timeout of 100 seconds&lt;br&gt;
• Parse the &lt;em&gt;albumID&lt;/em&gt; into an &lt;em&gt;ObjectID&lt;/em&gt; and handle errors if any.&lt;br&gt;
• Define a filter to find the album with the specified &lt;strong&gt;ObjectID&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;• Delete the album from the AlbCollection and handle any errors related to the deletion.&lt;br&gt;
• Check if any album was deleted and if no album was deleted (not found), return a not found response&lt;br&gt;
• Return a JSON response indicating that the album has been deleted&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Write the database logic
&lt;/h2&gt;

&lt;p&gt;In the database.go file first import the following packages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fUTyX-U6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4oyl7510f5gnfbqt1yk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fUTyX-U6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4oyl7510f5gnfbqt1yk.png" alt="Image description" width="675" height="267"&gt;&lt;/a&gt;&lt;br&gt;
Then add the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2aS-ES6q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1qumaza7xfvy16hmxze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2aS-ES6q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1qumaza7xfvy16hmxze.png" alt="Image description" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above has two functions that provide the logic for connecting to the mongo database and creating a reusable client instance and collection handler for a Go web application using the MongoDB Go driver respectively.&lt;br&gt;
The ConnectDB function establishes a connection to the MongoDB database and returns a client instance.&lt;br&gt;
• Create a new MongoDB client with the specified server URI and if there's an error, log it and exit the application&lt;br&gt;
• Create a context with a timeout of 100 seconds for the connection operation.&lt;br&gt;
• Connect the client to the MongoDB server and if there's an error, log it and exit the application.&lt;br&gt;
• Finally, print a message to indicate successful connection and return the client instance.&lt;/p&gt;

&lt;p&gt;Declare a global variable named &lt;strong&gt;&lt;em&gt;Client&lt;/em&gt;&lt;/strong&gt;, which is of type &lt;em&gt;**mongo.Client&lt;/em&gt;&lt;em&gt;. It assigns the client instance returned by the **ConnectDB&lt;/em&gt;* function to this variable. This allows other parts of your application to use the Client variable to interact with the MongoDB database.&lt;br&gt;
The &lt;strong&gt;OpenCollection&lt;/strong&gt; function creates and returns a MongoDB collection handler for the specified collection name.&lt;br&gt;
• Access the specified collection in the "&lt;strong&gt;gintest&lt;/strong&gt;" database using the client.&lt;br&gt;
• Return the collection handler.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;API endpoints and Routing
In your router.go file add:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U2326ZjS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x4jbznqcbwurpzs275r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U2326ZjS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x4jbznqcbwurpzs275r.png" alt="Image description" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above we define the routes and associate them with controller functions in a web application built using the Gin web framework.&lt;br&gt;&lt;br&gt;
The &lt;strong&gt;UserRoutes&lt;/strong&gt; function and takes one parameter, router, which is a pointer to a gin.Engine instance.&lt;br&gt;
It is responsible for defining the application's routes and associating them with the appropriate controller functions.&lt;br&gt;
These route definitions establish the API endpoints for your web application. When a request is made to one of these endpoints, Gin will call the associated controller function from the controllers package, which is where the actual request handling and business logic take place.&lt;/p&gt;

&lt;p&gt;Finally in the &lt;code&gt;main.go&lt;/code&gt; file add the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bCDFL2Fq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jgl6se97de6ex05pjor1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bCDFL2Fq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jgl6se97de6ex05pjor1.png" alt="Image description" width="652" height="445"&gt;&lt;/a&gt;&lt;br&gt;
The code above is the main entry point for a Go web application using the Gin web framework. It sets up the application's HTTP server, configures routes, and starts the server.&lt;br&gt;
• Create a new &lt;em&gt;Gin router&lt;/em&gt; instance&lt;br&gt;
• Add Gin's built-in &lt;strong&gt;logger&lt;/strong&gt; middleware for request logging&lt;br&gt;
• Configure routes for the application by calling the &lt;em&gt;UserRoutes&lt;/em&gt; function&lt;br&gt;
• Finally, start the HTTP server and listen on &lt;code&gt;port 8080&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Run the program:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1xo4muDd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/takymxgxv3m80w1vv8ew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1xo4muDd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/takymxgxv3m80w1vv8ew.png" alt="Image description" width="748" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It should display this in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YGIwf-RM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5x2g08hnyyiyag6cgvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YGIwf-RM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5x2g08hnyyiyag6cgvx.png" alt="Image description" width="800" height="36"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;7.Test the API using Postman&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Create a post request:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NmtVjl8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae2j2ad01yemi57i9ay9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NmtVjl8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae2j2ad01yemi57i9ay9.png" alt="Image description" width="530" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the raw JSON data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aF_Bllr6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3438kuj4f2bkilbmchk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aF_Bllr6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3438kuj4f2bkilbmchk.png" alt="Image description" width="586" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After sending the request the response should be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qPe7uH1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v507h5x37qzpgs9s993g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qPe7uH1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v507h5x37qzpgs9s993g.png" alt="Image description" width="186" height="33"&gt;&lt;/a&gt;&lt;br&gt;
Go ahead and add more data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get all albums&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fYg7e6ia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftrryb22cajzlea9s7fy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fYg7e6ia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftrryb22cajzlea9s7fy.png" alt="Image description" width="505" height="30"&gt;&lt;/a&gt;&lt;br&gt;
The result should be all the albums in the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hEhuVPMM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qp0yl7iaa2aoogzjxgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hEhuVPMM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qp0yl7iaa2aoogzjxgo.png" alt="Image description" width="714" height="302"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Update an album&lt;/strong&gt;&lt;br&gt;
Update the price of one album.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vWVXGlfi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1hux9cmbux1tw3hsea8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vWVXGlfi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1hux9cmbux1tw3hsea8.png" alt="Image description" width="800" height="55"&gt;&lt;/a&gt;&lt;br&gt;
The result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bVwWbRBO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzn0mfvoj424arzwf5k5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bVwWbRBO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzn0mfvoj424arzwf5k5.png" alt="Image description" width="411" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get album by id&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q3pPqqBF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6z68ppzzdkni7pqmrud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q3pPqqBF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6z68ppzzdkni7pqmrud.png" alt="Image description" width="800" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6AnleQzq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jayqsptzqzzo304grpvj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6AnleQzq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jayqsptzqzzo304grpvj.png" alt="Image description" width="577" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delete an album&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It should display:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hlx6ZXdl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34q1ja7swctrx1svxcai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hlx6ZXdl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34q1ja7swctrx1svxcai.png" alt="Image description" width="413" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The database:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IimXhRKI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwpvwkg1o6kzhe0ev8wn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IimXhRKI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwpvwkg1o6kzhe0ev8wn.png" alt="Image description" width="584" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;## Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This tutorial took you step by step through building a restful API using Gin framework and eventually testing using &lt;strong&gt;Postman&lt;/strong&gt;. To improve your Go skills also check out the &lt;a href="https://gin-gonic.com/docs/"&gt;official Gin Documentation.&lt;/a&gt; It contains advanced topics regarding the framework.&lt;br&gt;
For the MongoDB Go Driver check out my other &lt;a href="https://dev.to/wafulakevin/integrating-mongodb-in-golang-applications-1ehg"&gt;article&lt;/a&gt; on the same. It has some insightful and easily explained operations in MongoDB.&lt;br&gt;
Note: There are several other Go web frameworks i.e. &lt;a href="https://github.com/labstack/echo"&gt;Echo&lt;/a&gt; and &lt;a href="https://github.com/gofiber/fiber"&gt;Fiber&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Integrating MONGODB in GOLANG applications</title>
      <dc:creator>Kevin Wafula</dc:creator>
      <pubDate>Wed, 23 Aug 2023 19:22:18 +0000</pubDate>
      <link>https://dev.to/wafulakevin/integrating-mongodb-in-golang-applications-1ehg</link>
      <guid>https://dev.to/wafulakevin/integrating-mongodb-in-golang-applications-1ehg</guid>
      <description>&lt;p&gt;Learning how to integrate no-sql databases with applications is becoming a must-know skill for all developers out there. Golang in particular provides the &lt;a href="https://github.com/mongodb/mongo-go-driver" rel="noopener noreferrer"&gt;MongoDB Go Driver&lt;/a&gt; for easier and efficient connection with the mongo database.&lt;/p&gt;

&lt;p&gt;In this article, I'll help you understand integration of the mongo database into Golang applications using the official MONGODB Go Driver.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Getting started with MONGODB Go Driver&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CRUD operations with MongoDB&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go structs and MongoDB Mapping&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using Go structs to insert and Query MongoDB documents&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Some knowledge of Go&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Should have installed MongoDB(version 3.6 or higher) compass locally on your desktop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go version 1.13 or higher installed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An IDE i.e. Visual studio&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Getting started with MONGODB Go Driver&lt;/strong&gt;&lt;br&gt;
Let's install &lt;a href="https://github.com/mongodb/mongo-go-driver" rel="noopener noreferrer"&gt;MongoDB Go driver&lt;/a&gt; which has functions that allow for connection to the MongoDB local database and execution of queries and operations.&lt;/p&gt;

&lt;p&gt;The recommended method of installation is to use Go modules to install the dependency in your project. To install run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get go.mongodb.org/mongo-driver/mongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating a MongoDB client instance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a main.go file and import the mongoDB Go driver package using the import statement. Next, Create a mongo Client using the connect function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qlo3kzg1vrpr2m62vfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qlo3kzg1vrpr2m62vfm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advanced authentication and configuration can be found &lt;a href="https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Connect" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To check if a server has been found and connected use &lt;code&gt;ping&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5wevgvq9vlu94xac5nd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5wevgvq9vlu94xac5nd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a MongoDB Collection&lt;/strong&gt;&lt;br&gt;
Create collection instance retrieved from a &lt;strong&gt;user&lt;/strong&gt; Collection named &lt;strong&gt;mongo-practice&lt;/strong&gt;.If the database does not exist MongoDB will create it automatically when the above code is run.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fucexdm9xzk00ya5ncbr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fucexdm9xzk00ya5ncbr1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performing CRUD Operations with MongoDB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After connecting to the database and creating a Collection instance, we can now execute queries in our database using Go. This topic covers the crud(create, read, update and delete) operations.&lt;/p&gt;

&lt;p&gt;To perform the CRUD operations, you need to add the `&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
go.mongodb.org/mongo-driver/bson&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
`&lt;br&gt;
package to your import statement.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;go.mongodb.org/mongo-driver/bson&lt;/code&gt; package provides functionalities for working with BSON (Binary JSON) data, which is the encoding used to represent documents and data in MongoDB. BSON is a binary serialization format similar to JSON but designed for more efficient storage and traversal within the MongoDB database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inserting Documents in the Database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;InsertOne&lt;/strong&gt; executes an insert command to insert a single document into the collection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8ach3nv9l7bh4imvsfh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8ach3nv9l7bh4imvsfh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your terminal, run &lt;code&gt;go run main.go&lt;/code&gt;. It should display something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlxac7cuu3fryujj9wrr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlxac7cuu3fryujj9wrr.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
**InsertMany **executes an insert command to insert more than one document into the collection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi149dy36q8f5c7wdchm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi149dy36q8f5c7wdchm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The results should be as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fairm5sicwj0pjvc0meqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fairm5sicwj0pjvc0meqq.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbegm600pk4gcev3728l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbegm600pk4gcev3728l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read Documents from the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MongoDB provides FindOne() and Find() to retrieve documents from the database. FindOne() returns the first document that matches the filter while Find() returns all the documents that matches the filter.&lt;/p&gt;

&lt;p&gt;The filter parameter must be a document containing query operators and can be used to select which documents are included in the result. It cannot be nil. &lt;/p&gt;

&lt;p&gt;Opts parameter is used to specify the options.&lt;/p&gt;

&lt;p&gt;Define a filter to find documents where age is greater than 25:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6i9810a7p07a116cnmwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6i9810a7p07a116cnmwg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find all documents that match the filter and Iterate through the cursor and print each document&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futka77qfz8fv566fidxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futka77qfz8fv566fidxx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Retrieve the first document that matches the filter and display the results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkm8gibzdvrw6i6zj0x5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkm8gibzdvrw6i6zj0x5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;go run .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7hevamfn6vbs480u5o5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7hevamfn6vbs480u5o5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Documents in MongoDB&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MongoDB provides Update and Replace operations to change the documents in the database.&lt;/p&gt;

&lt;p&gt;Other operations to change documents in the mongoDB database include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;UpdateByID()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UpdateOne()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UpdateMany()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ReplaceOne()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;FindOneAndUpdate()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;FindOneAndReplace()&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;UpdateByID()&lt;/em&gt; updates a single document with a specified &lt;em&gt;ObjectID&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubdydqxqe1m0qnseoox5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubdydqxqe1m0qnseoox5.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Run&lt;code&gt;go main.go&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6wla0pwxartzc0tj0k2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6wla0pwxartzc0tj0k2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We first defined the document to insert and then Inserted the document into the "users" collection. Next Defined the update operation using the $set and $inc operators and finally updated the document using UpdateByID with the inserted document's ID.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UpdateOne() and UpdateMany()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, define a filter documents where "age" is greater than 25.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwckvt30ebtaedkky6exe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwckvt30ebtaedkky6exe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then define the update query using $set to update the "age" field:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxib4nm38lemtzh4i1xyt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxib4nm38lemtzh4i1xyt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execute the UpdateOne() function to update the first matching document&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fforjaijfbtpa71laxle9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fforjaijfbtpa71laxle9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execute the UpdateMany() function to update all matching documents&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmz146phj9ighsujkphha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmz146phj9ighsujkphha.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ReplaceOne()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define a filter to find the document with "name" equal to "Adrian"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpib90qz7yobrpbaf07q1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpib90qz7yobrpbaf07q1.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Create the replacement data&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgy70o19ern3yqggtrf67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgy70o19ern3yqggtrf67.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execute the ReplaceOne() function to replace the fields&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbshirov2pfidk05hv9pl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbshirov2pfidk05hv9pl.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Run &lt;code&gt;go run main.go&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxu20evexxufd73y8msvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxu20evexxufd73y8msvx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FindOneAndUpdate() and FindOneAndReplace() perform the same functions as FindOne() and ReplaceOne().The only difference is that they return the result before they update or replace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delete Documents from the database&lt;/strong&gt;&lt;br&gt;
DeleteOne and DeleteMany functions are provided by MongoDB to delete documents from the database.&lt;/p&gt;

&lt;p&gt;First, let's define a filter to find documents where age is greater than 25.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7g5retduq0hs8xbsf7b0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7g5retduq0hs8xbsf7b0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, the first document that matches the filter&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmb31hbzwh7ggecusoki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmb31hbzwh7ggecusoki.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, delete every document that matches the filter and display the number of documents deleted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6d68g5c2cpi10zqc37on.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6d68g5c2cpi10zqc37on.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go structs and MongoDB Mapping&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MongoDB Go driver allows for working with Go structs. You can map documents to Go structs for easier data migrations and operations.&lt;/p&gt;

&lt;p&gt;Here’s an example of struct matching the documents in the MongoDB database:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8y8u6y2yjvsun519evvh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8y8u6y2yjvsun519evvh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using structs to insert MongoDb Documents&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this case, we create a school instance and insert the instance into the collection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2z52z0uz72lz74q4fudi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2z52z0uz72lz74q4fudi.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The results after running main.go:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2bl9p5vcvssa1z8ixvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2bl9p5vcvssa1z8ixvg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Go structs to query MongoDb documents&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For easier data accessibility you can query the MongoDB databases with Go structs.&lt;/p&gt;

&lt;p&gt;First, execute a query to find documents that match the filter and move the cursor to the next document.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjai87n5d5cyhafluus2q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjai87n5d5cyhafluus2q.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, define a School struct to store the decoded document and decode the document from the cursor into the School struct.&lt;/p&gt;

&lt;p&gt;Finally,  print the information about the school&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tlewkdl01ctvq6ac9sx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tlewkdl01ctvq6ac9sx.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The result when you run &lt;code&gt;go run .&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1v8a8jvz50ynf3kfuuv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1v8a8jvz50ynf3kfuuv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Throughout the article, code examples, comments, and explanations provided a step-by-step guide for developers to follow along and understand the integration process. The article highlighted not only the technical aspects but also the importance of handling errors and checking for returned results.&lt;/p&gt;

&lt;p&gt;In conclusion, mastering the integration of MongoDB with Go applications using the MongoDB Go Driver empowers developers to build robust and efficient applications that leverage the power of NoSQL databases. This article serves as a comprehensive introduction to this integration, equipping developers with the knowledge to enhance their skill set and create effective database-driven solutions.&lt;/p&gt;

&lt;p&gt;For detailed and advanced concepts regarding &lt;a href="https://docs.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt; operations and aggregate functions ,you can head over to the &lt;a href="https://docs.mongodb.com/drivers/go/current/" rel="noopener noreferrer"&gt;MongoDB Driver documentation&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>UNDERSTANDING DEPENDENCIES IN GOLANG</title>
      <dc:creator>Kevin Wafula</dc:creator>
      <pubDate>Mon, 21 Aug 2023 14:04:40 +0000</pubDate>
      <link>https://dev.to/wafulakevin/understanding-dependencies-in-golang-fhk</link>
      <guid>https://dev.to/wafulakevin/understanding-dependencies-in-golang-fhk</guid>
      <description>&lt;p&gt;Golang relies on external packages. The packages are considered dependencies in your project. Go provides tools to manage this dependencies to allow you to replace or update during the life time of your project.&lt;/p&gt;

&lt;p&gt;In this article we’ll discuss:&lt;br&gt;
• Locating packages on &lt;a href="https://pkg.go.dev/"&gt;pkg.go.dev&lt;/a&gt;&lt;br&gt;
• Importing the packages&lt;br&gt;
• Enable dependency tracking by adding your code to a module.&lt;br&gt;
• Add and manage external packages to your module.&lt;br&gt;
• How to upgrade and downgrade versions of the imported dependencies&lt;br&gt;
• Golang commands used to make dependency changes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependencies as modules&lt;/strong&gt;&lt;br&gt;
Managing dependencies is a critical aspect of building projects in Go. Go provides tools that manage dependencies as modules that contains the imported packages. For efficient management of dependencies Golang provides:&lt;br&gt;
• A documentation browser and a package search engine (&lt;a href="https://pkg.go.dev/"&gt;pkg.go.dev&lt;/a&gt;).&lt;br&gt;
• Go tools for upgrading, downgrading and managing package sources&lt;br&gt;
• A decentralized system for publishing modules and retrieving their code. Developers use version numbers and repository to achieve this.&lt;/p&gt;

&lt;p&gt;Locating and importing the packages&lt;br&gt;
Search &lt;a href="https://pkg.go.dev/"&gt;pkg.go.dev&lt;/a&gt; to find the packages that contain the functions you need.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_F6PgNkl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9kg2uudfjzuqlxldo38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_F6PgNkl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9kg2uudfjzuqlxldo38.png" alt="Image description" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find the package path and copy. Paste the path in your import statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "golang.org/x/sync/errgroup"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there’s more than one package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import(
"golang.org/x/sync/errgroup"
"github.com/mitchellh/iochan"
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dependency Tracking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To enable tacking and management of your dependencies you have to put your code in a module. This creates a go.mod file at the root of your of directory. All the dependencies you’ll add will be listed in the _*&lt;em&gt;go.mod *&lt;/em&gt;_file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c76KhHlu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1fz83kuidx57xfbbf7y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c76KhHlu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1fz83kuidx57xfbbf7y.png" alt="Image description" width="800" height="35"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The go.mod file looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XVdZr4xc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r31yhlhmgcr0d3cc2xm2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XVdZr4xc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r31yhlhmgcr0d3cc2xm2.png" alt="Image description" width="800" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`go mod tidy`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to add module requirements and sums. It also removes dependencies to the packages you’re no longer using.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Naming of Modules&lt;/strong&gt;&lt;br&gt;
To run &lt;code&gt;go mod init&lt;/code&gt; you need to specify a module path which also serves as the module’s name. Naming modules in Go is required to take the following form: &lt;em&gt;/&lt;/em&gt;. The prefix describes the module. In this case we’re using github.com as it is the best practice for modules you intend to publish for others to use. Descriptive text is the name of the project.&lt;br&gt;
Reserved module prefixes&lt;br&gt;
• example – used in Go documentation and tutorials as a module path i.e. &lt;em&gt;example.com/kevin&lt;/em&gt;&lt;br&gt;
• test – only used for a module whose code is used to locally test functions in another module i.e. &lt;code&gt;go mod init test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding a Dependency&lt;/strong&gt;&lt;br&gt;
To add a dependency for a package in your module run go get . In the case of a specific dependency, you need to specify its module path. I.e. &lt;code&gt;go get example.com/themodule&lt;/code&gt; .This command authenticates each module it downloads and makes sure it is unchanged. In case of any changes it will provide a security error.&lt;br&gt;
You can also get a specific version of a module by specifying the version of the module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`go get example.com/themodule@v1.4`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or the latest version by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`go get example.com/themodule@latest`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Checking for available updates&lt;/strong&gt;&lt;br&gt;
Use the &lt;code&gt;go list&lt;/code&gt; command to display all your module dependencies. The command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`go list –m –u all`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;lists all the dependences of your current module with their latest version.&lt;/p&gt;

&lt;p&gt;In the above article I sought to summarize all the important information regarding dependencies. There are other detailed aspects of dependencies like developing and testing against unpublished code and getting a specific commit using a repository Identifier that are covered in detail by &lt;a href="https://go.dev/doc/modules/managing-dependencies"&gt;Go official documentation.&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SENDING EMAILS IN DJANGO: A BEGINNER'S GUIDE</title>
      <dc:creator>Kevin Wafula</dc:creator>
      <pubDate>Fri, 14 Apr 2023 07:45:43 +0000</pubDate>
      <link>https://dev.to/wafulakevin/sending-emails-in-django-a-beginners-guide-1g1i</link>
      <guid>https://dev.to/wafulakevin/sending-emails-in-django-a-beginners-guide-1g1i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Sending Emails in Django.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Django is a popular Python web framework that provides a robust set of tools for building web applications. One key feature of web applications is the ability to send and receive email messages. Sending email messages in Django can be easily accomplished by built-in SMTP and EmailMessage classes. In this article, I'll introduce you to sending emails in Django and the best practices for going about it, which will accelerate and make email delivery easier.&lt;/p&gt;

&lt;p&gt;In addition to the built-in &lt;strong&gt;&lt;em&gt;EmailMessage&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;SMTP&lt;/em&gt;&lt;/strong&gt; classes, Django includes support for other email-related features, such as email templates, email attachments, and email logging. With these tools, developers can customize the look and feel of their email messages, attach files to their emails, and track email activity for debugging and analysis purposes. By using Django's email-related features, developers can build web applications that are not only powerful and feature-rich but also capable of sending and receiving email messages with ease. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You’ll be best equipped for this tutorial if you already have a solid foundation in some web application concepts. You should understand how &lt;a href="https://www.geeksforgeeks.org/django-request-and-response-cycle-httprequest-and-httpresponse-objects/"&gt;HTTP requests and responses&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment"&gt;create a Django development environment and start a Django project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using **send_mail()&lt;/strong&gt; to send email messages**&lt;/p&gt;

&lt;p&gt;Let's kick off our tutorial with a simple code to show how to send an email.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TvF2GC_x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7mdi6y2h3x1o9eo2x6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TvF2GC_x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7mdi6y2h3x1o9eo2x6d.png" alt="Image description" width="701" height="185"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here is a detailed list of parameters of send_mail ():&lt;br&gt;
• &lt;strong&gt;&lt;em&gt;subject&lt;/em&gt;&lt;/strong&gt;- the subject of your email&lt;br&gt;
• &lt;strong&gt;&lt;em&gt;from_email&lt;/em&gt;&lt;/strong&gt; – the sender&lt;br&gt;
• &lt;strong&gt;&lt;em&gt;recipient_list&lt;/em&gt; *&lt;em&gt;– the receiver&lt;br&gt;
• *&lt;/em&gt;_message _&lt;/strong&gt;– the body of the email&lt;br&gt;
• &lt;strong&gt;&lt;em&gt;fail_silently&lt;/em&gt;&lt;/strong&gt; – Boolean, If True send_mail will not raise an exception error.&lt;br&gt;
• &lt;strong&gt;&lt;em&gt;html_message&lt;/em&gt;&lt;/strong&gt; – if provided the resulting email will be an alternative email message as &lt;em&gt;text/plain&lt;/em&gt; content type while the html message will be the &lt;em&gt;text/html&lt;/em&gt; content type.&lt;br&gt;
The mail is sent using the SMTP host and port specified in the settings.py of the main project. To authenticate the SMTP server &lt;a href="https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-EMAIL_HOST_USER"&gt;EMAIL_HOST_USER&lt;/a&gt; and &lt;a href="https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-EMAIL_HOST_PASSWORD"&gt;EMAIL_HOST_PASSWORD&lt;/a&gt;  are used. &lt;a href="https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-EMAIL_USE_TLS"&gt;EMAIL_USE_TLS&lt;/a&gt; and &lt;a href="https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-EMAIL_USE_SSL"&gt;EMAIL_USE_SSL&lt;/a&gt; are then used to provide a secure connection. DEFAULT_FROM_EMAIL is used to provide a default sender email.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S5lk8ULY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x55u07uwrj9kqash2966.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S5lk8ULY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x55u07uwrj9kqash2966.png" alt="Image description" width="684" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sending Mass emails using &lt;em&gt;send_mass_mail&lt;/em&gt;()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is more or less the same as send_mail () but here you have to use a datatuple parameter that holds the messages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rRWKN71s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w57xbw6xx45k3wy405k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rRWKN71s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w57xbw6xx45k3wy405k.png" alt="Image description" width="708" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main difference between send_mail () and send_mass_mail () is that send_mail () opens a connection with the SMTP server each time it is executed while send_mass_mail () uses a single connection to send all its messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using _EmailMessage _class to send emails&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both &lt;em&gt;send_mail()&lt;/em&gt; and &lt;em&gt;send_mass_mail()&lt;/em&gt; make use of EmailMessage class. But most of the &lt;em&gt;EmailMessage&lt;/em&gt; features are not available to send_mail ().&lt;br&gt;
_EmailMessage _is responsible for creating the email message itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D2ll0Te4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nk8vb5enh0779fvnscua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D2ll0Te4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nk8vb5enh0779fvnscua.png" alt="Image description" width="604" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The parameters include &lt;em&gt;body, from_email (), to, bcc, attachments, headers, cc, and reply_to&lt;/em&gt; in that order.&lt;br&gt;
The EmailMessage also has methods like &lt;em&gt;recipients (), attach ()&lt;/em&gt;, and &lt;a href="https://docs.djangoproject.com/en/4.2/topics/email/#topic-email-backends"&gt;many more&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sending emails to the console or terminal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;During development in Django, it can be useful to send email messages to the console or terminal instead of sending them to real email addresses. This allows developers to test email functionality without the need for a real email server or the risk of accidentally sending emails to real users. Luckily, Django provides a simple way to redirect email messages to the console using the &lt;strong&gt;EmailBackend&lt;/strong&gt; class.&lt;br&gt;
To use the &lt;strong&gt;EmailBackend&lt;/strong&gt; class, developers can update the &lt;em&gt;EMAIL_BACKEND&lt;/em&gt; setting in their Django settings file to point to the console backend:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4GiGs6iV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8b29sirg8e3ewlddyuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4GiGs6iV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8b29sirg8e3ewlddyuo.png" alt="Image description" width="631" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using the console backend, developers can easily test their email functionality during development without the need for a real email server or the risk of accidentally sending emails to real users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, sending emails in Django is a straightforward process that can be accomplished using the built-in _EmailMessage _and _SMTP _classes. By leveraging these classes and other email-related features provided by Django, developers can send personalized email messages to users, notify administrators of errors or system events, and perform other email-related tasks.&lt;/p&gt;

&lt;p&gt;During development, Django also provides a convenient way to redirect email messages to the console or terminal, allowing developers to test their email functionality without the need for a real email server or the risk of accidentally sending emails to real users.&lt;/p&gt;

&lt;p&gt;Overall, Django's email-related features provide a powerful and flexible toolset for building web applications that are capable of sending and receiving email messages with ease. Whether you're sending simple text emails or complex HTML messages with attachments, Django's email functionality can help you communicate with your users effectively and efficiently.&lt;br&gt;
&lt;strong&gt;Attributions&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Credits&lt;/strong&gt;&lt;br&gt;
• &lt;a href="https://docs.djangoproject.com/en/4.2/topics/email/#topic-email-backends"&gt;Django Documentation&lt;/a&gt;&lt;br&gt;
• &lt;a href="https://www.geeksforgeeks.org/django-request-and-response-cycle-httprequest-and-httpresponse-objects/"&gt;Geeks for geeks&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
