{"id":3626,"date":"2022-10-26T14:31:42","date_gmt":"2022-10-26T14:31:42","guid":{"rendered":"https:\/\/www.reloadly.com\/blog\/?p=3626"},"modified":"2022-10-26T14:35:04","modified_gmt":"2022-10-26T14:35:04","slug":"building-web-applications-with-go","status":"publish","type":"post","link":"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/","title":{"rendered":"Building Web applications with Go"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_46 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"ez-toc-toggle-icon-1\"><label for=\"item-69ed2df654cc3\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input  type=\"checkbox\" id=\"item-69ed2df654cc3\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-visibility-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Creating_a_Go_module\" title=\"Creating a Go module\">Creating a Go module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Executing_your_Go_module\" title=\"Executing your Go module\">Executing your Go module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Creating_your_HTML_Template\" title=\"Creating your HTML Template\">Creating your HTML Template<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Creating_your_HTTP_Server\" title=\"Creating your HTTP Server\">Creating your HTTP Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Working_with_Reloadlys_SDK\" title=\"Working with Reloadly\u2019s SDK\">Working with Reloadly\u2019s SDK<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Making_a_Top-up\" title=\"Making a Top-up\">Making a Top-up<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Styling\" title=\"Styling\">Styling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.reloadly.com\/blog\/building-web-applications-with-go\/#Summary\" title=\"Summary\">Summary<\/a><\/li><\/ul><\/nav><\/div>\n\n<div style=\"height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>A lot of web development is centered around languages such as JavaScript and PHP. Developers focused on using Go as a stack may take longer when trying to create a web application from scratch. This article will teach you how to:<\/p>\n\n\n\n<ul><li>Create a web application with Go from scratch<\/li><li>Create a HTML template that sends data to your Go application<\/li><li>Adding remote modules as dependencies into your application<\/li><li>Work with data from your HTML template to make API requests in your application<\/li><\/ul>\n\n\n\n<div style=\"height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_Go_module\"><\/span>Creating a Go module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A <a href=\"https:\/\/go.dev\/blog\/using-go-modules\" target=\"_blank\" rel=\"noreferrer noopener\">module<\/a> is the starting point of every Go application. To create a module from scratch, navigate to your terminal and first create the directory your module source code will be in<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir reloadly-project\ncd reloadly-project<\/pre>\n\n\n\n<p>&nbsp;Then initialize your module using the go.mod init &nbsp;command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">go mod init airtime<\/pre>\n\n\n\n<p>This command will return the following output when creating the module:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Output\ngo: creating new go.mod: airtime airtime<\/pre>\n\n\n\n<p>Once the module has been created, your project\u2019s directory structure should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"204\" height=\"82\" src=\"https:\/\/www.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-140448.png\" alt=\"\" class=\"wp-image-3637\" \/><\/figure>\n\n\n\n<p>The go.mod &nbsp;file is responsible for tracking any remote dependencies your code will use as well as any other information about your module. Currently, all it contains is your module name and the version of Go you\u2019re working with:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ reloadly-project\/airtime\/go.mod\n\nmodule airtime\ngo 1.16<\/pre>\n\n\n\n<p>In Go, modules are distributed from version control repositories (usually Git repositories). You can add other modules as dependencies by referencing the repository path of the module.&nbsp;<br><\/p>\n\n\n\n<p>To enable your web application to make airtime top-ups, you need Reloadly\u2019s Airtime Go SDK as a dependency. Navigate to your terminal and add the SDK to your module:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">go get -u github.com\/reloadly\/reloadly-sdk-golang<\/pre>\n\n\n\n<p>Once this is done, your &nbsp;go.mod &nbsp;file should be updated with the details of the SDK<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">module airtime\n\ngo 1.16\n\nrequire github.com\/reloadly\/reloadly-sdk-golang v1.0.1 <\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Executing_your_Go_module\"><\/span>Executing your Go module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To make your module an actual program that can be executed, you need to create a file where your code\/logic can be written. In your &nbsp;airtime &nbsp;module directory, create a <code>main.go<\/code> file and add the code snippet below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n        fmt.Println(\"Hello, World!\")\n}<\/pre>\n\n\n\n<p>In the above code snippet, you made use of the following:<\/p>\n\n\n\n<ul><li><code>main<\/code>: A package that works with Go\u2019s compiler to turn your module to an executable program.<\/li><li><code>fmt<\/code>: A package that formats basic strings and values. It is mainly used for printing information to the terminal.<\/li><li><code>main()<\/code>: &nbsp;A function that serves as the entry point of your module. Once your program is run, Go will look for this function and execute any code in it.<\/li><\/ul>\n\n\n\n<p>To execute your Go module, run the <code>go run<\/code> command in your terminal just like the snippet below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">go run main.go<\/pre>\n\n\n\n<p>You should see the following message in your terminal<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Hello, World!<\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_your_HTML_Template\"><\/span>Creating your HTML Template<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Your next step is to create a HTML template where users can view and interact with. In the root directory of your project, create a folder named templates and in this folder, create a file named airtime.html<br>Remember you are building a web app that can send airtime top-ups. In airtime.html, create a form that receives inputs of the details necessary to make an airtime top-up:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  &lt;!-- templates\/airtime.html --&gt;\n\n&lt;div&gt;\n   {{if .Success}}\n   &lt;div&gt;\n      &lt;p&gt;Your top-up is successful!&lt;\/p&gt;\n   &lt;\/div&gt;\n   {{else}}\n   &lt;div&gt;\n      &lt;h1&gt;Topup Form&lt;\/h1&gt;\n   &lt;\/div&gt;\n   &lt;div&gt;&lt;\/div&gt;\n   &lt;form action=\"\/airtime.html\" method=\"POST\"&gt;\n      &lt;input\n         type=\"text\" placeholder=\"your operator's id\" name=\"operatorid\"\n         \/&gt;\n      &lt;input\n         type=\"text\" placeholder=\"your top-up amount (NGN)\" name=\"amount\"\n         \/&gt;\n      &lt;input\n         type=\"text\" placeholder=\"your unique reference\" name=\"reference\"\n         \/&gt;\n      &lt;input\n         type=\"text\" placeholder=\"recipient's phone number\" name=\"number\"\n         \/&gt;\n      &lt;input type=\"text\" placeholder=\"recipient's country code\" name=\"countrycode\"\/&gt;\n      &lt;input  type=\"submit\" value=\"send\" \/&gt;\n   &lt;\/form&gt;\n   {{end}}\n&lt;\/div&gt;<\/pre>\n\n\n\n<p>In the code snippet above, an inline expression is added to communicate with your web server and choose what to display based on what stage of making the top-up you are in. This inline expression will be referenced in your web server as well.<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_your_HTTP_Server\"><\/span>Creating your HTTP Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Every HTTP server written in Go should contain at least two major functions:<\/p>\n\n\n\n<ul><li><code>http.HandleFunc<\/code>: This function is responsible for telling the server how HTTP requests made to a specific route should be handled<\/li><li><code>http.ListenAndServe<\/code>: This function starts the server, listens for any changes or requests from <code>http.HandleFunc<\/code> &nbsp;and executes them.<\/li><\/ul>\n\n\n\n<p>In your <code>main.go<\/code> &nbsp;file, initiate the <code>http.HandleFunc<\/code> function to make a request to the <code>\"\/\"<\/code> route :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\npackage main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n    \"html\/template\"\n)\n\nfunc main() {\n    tmpl, err := template.ParseFiles(\".\/templates\/airtime.html\")\n    if err != nil {\n        log.Fatal(\"Parse: \", err)\n        return\n    }\n\n    http.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\n        if r.Method != http.MethodPost {\n            tmpl.Execute(w, nil)\n            return\n        }\n\n       fmt.Println(\"Server works!\")\n    })\n    http.ListenAndServe(\":8000\", nil)\n}\n<\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the code snippet above:<\/p>\n\n\n\n<ul><li>The <code>net\/http<\/code> &nbsp;and <code>html\/template<\/code> packages are imported to handle HTTP implementations and render HTML templates respectively.&nbsp;<\/li><li>&nbsp;In the <code>main()<\/code> function, the <code>ParseFiles()<\/code> function is used to securely parse the &nbsp;<code>airtime.html<\/code> &nbsp;template.&nbsp;<\/li><li>The <code>handleFunc()<\/code> function makes use of the <code>ResponseWriter<\/code> interface to construct HTTP responses for any request that will be received by the server.&nbsp;<\/li><li>The Request &nbsp;interface represents the HTTP request details that will be sent to the server.<\/li><li>A conditional is specified for the type of HTTP request to expect, the parsed template (<code>airtime.html<\/code>) to be executed in the request and its output written to Go\u2019s <a target=\"_blank\" href=\"https:\/\/pkg.go.dev\/github.com\/VertebrateResequencing\/wr\" rel=\"noreferrer noopener\">workflow runner<\/a><\/li><\/ul>\n\n\n\n<p>As a confirmation that the server receives the parsed HTML template, the message <code>\"Server works!\"<\/code> &nbsp;is printed out on the terminal once a successful request is sent when the server is live. Execute your server via the <code>go run<\/code> &nbsp;command in the terminal and navigate to <code>localhost:8000<\/code> on your browser. You should see your airtime top-up form:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"516\" src=\"https:\/\/www.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-25-144154-1024x516.png\" alt=\"\" class=\"wp-image-3628\" srcset=\"https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-25-144154-1024x516.png 1024w, https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-25-144154-300x151.png 300w, https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-25-144154-768x387.png 768w, https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-25-144154-1536x774.png 1536w, https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-25-144154.png 1918w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Clicking on the <code>send<\/code> button will redirect the page to <code>localhost:8000\/airtime.html<\/code> which will be blank as the inline expressions set in your HTML template have not yet been referenced in your server. You should also get the message that says &nbsp;<code>Server works!<\/code> &nbsp;in your terminal.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PS C:\\Users\\ugwur\\reloadly-airtime-form-golang&gt; go run main.go\nServer works!\n<\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Working_with_Reloadlys_SDK\"><\/span>Working with Reloadly\u2019s SDK<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To access the APIs needed to send airtime top-ups successfully, you will need to install Reloadly\u2019s SDK in your project:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">go get -u github.com\/reloadly\/reloadly-sdk-golang\n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>You will also need client credentials for securing any top-up you make. Steps on how to get them are in <\/em><a target=\"_blank\" href=\"https:\/\/www.reloadly.com\/blog\/airtime-go-quickstart\/\" rel=\"noreferrer noopener\"><em>Reloadly\u2019s Go QuickStart<\/em><\/a><\/p><\/blockquote>\n\n\n\n<p>Once the installation is complete, import the SDK to your main module. Also create a struct that mirrors the form input details of your HTML template:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\npackage main\nimport (\n    \"fmt\"\n    \"html\/template\"\n    \"log\"\n    \"net\/http\"\n    reloadly \"github.com\/reloadly\/reloadly-sdk-golang\/airtime\"\n)\ntype TopupDetails struct {\n    OperatorID  string\n    Amount      string\n    Reference   string\n    Number      string\n    CountryCode string\n}\n\n\/\/ ... <\/pre>\n\n\n\n<p>Next, you need an access token to authenticate every top-up request made to your server. Access tokens can be generated using Reloadly\u2019s <code>NewClient()<\/code> function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\n\/\/ ...\n\nfunc main() {\n\n\/\/ ...\n\n  rAccessToken, err := reloadly.NewClient(\"YOUR_CLIENT_ID_HERE\", \"YOUR_CLIENT_SECRET_H   ERE\", true)\n    if err != nil {\n        fmt.Println(err)\n    }\n\n\/\/ ...\n\n}\n\/\/ ...<\/pre>\n\n\n\n<p>Once this is done, you need to link all user input from the HTML template to the top-up request you will make with Reloadly\u2019s SDK:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\n\/\/ ...\n\nfunc main() {\n\n\/\/ ...\n\n  http.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\n    \/\/ ...\n        rTopupDetails := reloadly.Topuprequest{\n            Amount:     r.FormValue(\"amount\"),\n            OperatorID: r.FormValue((\"operatorid\")),\n        }\n        rCustomIdentifier := reloadly.AddCustomIdentifier(r.FormValue(\"reference\"))\n        rPhone := reloadly.Phone{\n            Number:      r.FormValue(\"number\"),\n            CountryCode: r.FormValue((\"countrycode\")),\n        }\n    \/\/ ...\n  })\n\n\/\/ ...\n\n}\n\/\/ ...<\/pre>\n\n\n\n<p>Lastly, you need to create a top-up function that sends a request using all the details stored in the <code>rAccessToken<\/code>, <code>rTopupDetails<\/code>, <code>rCustomIdentifier<\/code>, and <code>rPhone<\/code> variables.&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\/\/ ...\n\nimport (\n\/\/ ...\n    \"encoding\/json\"\n\/\/ ...\n)\n\nfunc main() {\n\/\/ ...\n  http.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\n    \/\/ ...\n    rTopUp, err := rAccessToken.Topup(rTopupDetails.Amount, rTopupDetails.OperatorID,       true, rPhone, rCustomIdentifier)\n        if err != nil {\n            fmt.Println(err)\n        }\n       jsonResp, err := json.Marshal(rTopUp)\n        if err != nil {\n            log.Fatalf(\"Error happened in JSON marshal. Err: %s\", err)\n        }\n        fmt.Printf(\"json data: %s\\n\", jsonResp)\n        tmpl.Execute(w, struct{ Success bool }{true})\n    \/\/ ...\n  })\n  http.ListenAndServe(\":8000\", nil)\n\/\/ ...\n}\n\n\/\/ ...<\/pre>\n\n\n\n<p>Convert the response gotten from your request to JSON with the json.Marshal() function. Then use the Execute function to call the inline expression specified in your HTML template. When you are done, your main.go &nbsp;file should look like this <a target=\"_blank\" href=\"https:\/\/go.dev\/play\/p\/myeUndQTV9Y\" rel=\"noreferrer noopener\">playground<\/a><\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Making_a_Top-up\"><\/span>Making a Top-up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>At this point, your server should be complete. Start the server, navigate to <code>localhost:8000<\/code> &nbsp;and attempt a top-up<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Building web applications with Go and Reloadly\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/IZcBlnrMOyA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>To confirm that your top-up was successful, response details will also be logged on your server:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">C:\\Users\\ugwur\\reloadly-airtime-form-golang&gt; go run main.go\n\njson data: {\"transactionId\":44380,\"operatorTransactionId\":null,\"customIdentifier\":\"airtime-100001\",\"recipientPhone\":\"2348147658720\",\"recipientEmail\":null,\"senderPhone\":\"\",\"countryCode\":\"NG\",\"operatorId\":341,\"operatorName\":\"MTN Nigeria\",\"discount\":0,\"discountCurrencyCode\":\"NGN\",\"requestedAmount\":100,\"requestedAmountCurrencyCode\":\"NGN\",\"deliveredAmount\":100,\"deliveredAmountCurrencyCode\":\"NGN\",\"transactionDate\":\"2022-10-25 21:01:09\",\"pinDetail\":null,\"balanceInfo\":{\"cost\":100,\"currencyCode\":\"NGN\",\"currencyName\":\"Nigerian Naira\",\"newBalance\":733733.01091,\"oldBalance\":733833.01091,\"updatedAt\":\"2022-10-26 01:01:09\"}}<\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Styling\"><\/span>Styling<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Applying this section to your project may be optional. To apply styling to a HTML template, create a separate folder named <code>css<\/code> &nbsp;and in it, create your CSS file.<\/p>\n\n\n\n<p>Import this into your HTML template as you normally would but add an extra forward slash. This is because Go will only render a file if it\u2019s directory is registered in a Go module. After creating your CSS file, your project\u2019s folder structure should look something like this:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"157\" src=\"https:\/\/www.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-135747.png\" alt=\"\" class=\"wp-image-3635\" \/><\/figure>\n<\/div><\/div>\n\n\n\n<p>Then in your <code>main.go<\/code> file, register the CSS file using the http <code>Handle()<\/code> &nbsp;function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \/airtime\/main.go\n\/\/ ...\n\nimport (\n\/\/ ...\n    \"encoding\/json\"\n\/\/ ...\n)\n\nfunc main() {\n\/\/ ...\n  http.Handle(\"\/css\/\", http.StripPrefix(\"\/css\", http.FileServer((http.Dir(\"css\")))))\n\n  http.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\n    \/\/ ...\n       \/\/ HTTP methods and top-up requests go here\n    \/\/ ...\n  })\n  http.ListenAndServe(\":8000\", nil)\n\/\/ ...\n}\n\n\/\/ ...<\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we reviewed how to use Go in building web applications. We also went a step further to integrate REST APIs in our application. Should you need the full repository of this tutorial\u2019s project, you can find it on <a target=\"_blank\" href=\"https:\/\/github.com\/Reloadly\/reloadly-airtime-form-golang\" rel=\"noreferrer noopener\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Popular for its execution speed, Go is a programming language mainly used for backend development. It can also be used to develop minimalistic web apps. This blog post aims to show you how to develop a web application for making airtime top-ups using GO, HTML, and Reloadly\u2019s SDK. <\/p>\n","protected":false},"author":17,"featured_media":3645,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[11,412,413,275,439,440],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Building Web applications with Go - Reloadly<\/title>\n<meta name=\"description\" content=\"Reloadly Resources. Insights, Info and Analysis For The Mobile Airtime API Community\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Building Web applications with Go - Reloadly\" \/>\n<meta property=\"og:description\" content=\"Reloadly Resources. Insights, Info and Analysis For The Mobile Airtime API Community\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/\" \/>\n<meta property=\"og:site_name\" content=\"Reloadly\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-26T14:31:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-26T14:35:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Banner-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1600\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"8 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#organization\",\"name\":\"Reloadly\",\"url\":\"https:\/\/blog.reloadly.com\/blog\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2020\/11\/logo-1.svg\",\"width\":100,\"height\":100,\"caption\":\"Reloadly\"},\"image\":{\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#website\",\"url\":\"https:\/\/blog.reloadly.com\/blog\/\",\"name\":\"Reloadly\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/blog.reloadly.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/blog.reloadly.com\/blog\/wp-content\/uploads\/2022\/10\/Banner-1.jpg\",\"width\":1600,\"height\":900,\"caption\":\"A banner containing the logos of HTML5, Go, and Reloadly\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#webpage\",\"url\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/\",\"name\":\"Building Web applications with Go - Reloadly\",\"isPartOf\":{\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#primaryimage\"},\"datePublished\":\"2022-10-26T14:31:42+00:00\",\"dateModified\":\"2022-10-26T14:35:04+00:00\",\"description\":\"Reloadly Resources. Insights, Info and Analysis For The Mobile Airtime API Community\",\"breadcrumb\":{\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.reloadly.com\/blog\/\",\"url\":\"https:\/\/blog.reloadly.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/\",\"url\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/\",\"name\":\"Building Web applications with Go\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#webpage\"},\"author\":{\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#\/schema\/person\/2400042054e3b3009489e487224a6984\"},\"headline\":\"Building Web applications with Go\",\"datePublished\":\"2022-10-26T14:31:42+00:00\",\"dateModified\":\"2022-10-26T14:35:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#webpage\"},\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#primaryimage\"},\"keywords\":\"airtime,go,golang,reloadly,sdk,web\",\"articleSection\":\"Developer Blog\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.reloadly.com\/blog\/building-web-applications-with-go\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#\/schema\/person\/2400042054e3b3009489e487224a6984\",\"name\":\"Raphael Ugwu\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/blog.reloadly.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/656844c0101d37e5695eee915ed3a49d?s=96&d=mm&r=g\",\"caption\":\"Raphael Ugwu\"},\"url\":\"https:\/\/blog.reloadly.com\/blog\/author\/fullstackmafia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","authors":[{"term_id":359,"user_id":17,"is_guest":0,"slug":"fullstackmafia","display_name":"Raphael Ugwu"}],"_links":{"self":[{"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/posts\/3626"}],"collection":[{"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/comments?post=3626"}],"version-history":[{"count":12,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/posts\/3626\/revisions"}],"predecessor-version":[{"id":3644,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/posts\/3626\/revisions\/3644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/media\/3645"}],"wp:attachment":[{"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/media?parent=3626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/categories?post=3626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.reloadly.com\/blog\/wp-json\/wp\/v2\/tags?post=3626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}