<?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: Udaya Prakash</title>
    <description>The latest articles on DEV Community by Udaya Prakash (@udaya2899).</description>
    <link>https://dev.to/udaya2899</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%2F515721%2F0d3a954b-0fb0-4bea-954a-36a7557e27f5.jpeg</url>
      <title>DEV Community: Udaya Prakash</title>
      <link>https://dev.to/udaya2899</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/udaya2899"/>
    <language>en</language>
    <item>
      <title>Creating an HTTP server in Go using gin</title>
      <dc:creator>Udaya Prakash</dc:creator>
      <pubDate>Mon, 16 Nov 2020 20:48:02 +0000</pubDate>
      <link>https://dev.to/udaya2899/creating-an-http-server-in-go-using-gin-2cfh</link>
      <guid>https://dev.to/udaya2899/creating-an-http-server-in-go-using-gin-2cfh</guid>
      <description>&lt;p&gt;Let's get straight to the business with some minimal introduction and motivation.&lt;/p&gt;

&lt;p&gt;A web framework makes the life of a backend developer easier. If you want to set up a server in Go, you have a handful of options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/julienschmidt/httprouter" rel="noopener noreferrer"&gt;httprouter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/astaxie/beego" rel="noopener noreferrer"&gt;beego&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/valyala/fasthttp" rel="noopener noreferrer"&gt;fasthttp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/gin-gonic/gin" rel="noopener noreferrer"&gt;gin&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and the list goes on...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/julienschmidt/httprouter" rel="noopener noreferrer"&gt;httprouter&lt;/a&gt; is a fast router for Go that implements a lot of features of which a few are Path auto-correction, Zero Garbage, Best Performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not use httprouter?
&lt;/h2&gt;

&lt;p&gt;Although httprouter provides a lot of these features in routing, it's not a complete framework i.e. it does not provide features that improve developer experience like middleware.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, &lt;a href="https://github.com/gin-gonic/gin" rel="noopener noreferrer"&gt;gin&lt;/a&gt; it is
&lt;/h2&gt;

&lt;p&gt;With gin, it's simple and we get more features like adding middleware later on for authentication, logging and authorization. Without further ado, let's &lt;code&gt;go&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a folder &lt;code&gt;gin-server&lt;/code&gt; in your &lt;code&gt;$GOPATH&lt;/code&gt; and enter the folder &lt;code&gt;gin-server&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initiate go modules using the command in your shell:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; Import &lt;code&gt;gin&lt;/code&gt; using the command in your shell:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get -u github.com/gin-gonic/gin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a file named &lt;code&gt;main.go&lt;/code&gt; with the following contents&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    // initiates a gin Engine with the default logger and recovery middleware
    router := gin.Default()

    // sets up a GET API in route /hello that returns the text "World"
    router.GET("/hello", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "message": "World",
        })
    })

    // Run implements a http.ListenAndServe() and takes in an optional Port number
    // The default port is :8080
    router.Run()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we're ready to &lt;code&gt;go&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;All we need is to run the server with the command in the project directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should see these logs on the terminal &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%2Fi%2Fx6yzve2e0xfdh8vyul2w.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%2Fi%2Fx6yzve2e0xfdh8vyul2w.png" alt="Go run logs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open your favorite browser and enter the address to see the response of the API we built&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%2Fi%2Ffxkjh7gn792odoistt6e.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%2Fi%2Ffxkjh7gn792odoistt6e.png" alt="Testing API in Browser"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes, we're done! 🎉&lt;/p&gt;

&lt;p&gt;We've set up an HTTP Server with the &lt;code&gt;gin&lt;/code&gt; framework. In the future, expect articles on connecting to Databases, best practices, authentication, middleware, and more. 🤟&lt;/p&gt;

</description>
      <category>go</category>
      <category>devjournal</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
