<?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>Forem: Sawit Trisirisatayawong</title>
    <description>The latest articles on Forem by Sawit Trisirisatayawong (@tansawit).</description>
    <link>https://forem.com/tansawit</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%2F233298%2F8fc0dd28-0cf4-407c-a1b6-046e3ceeea68.jpeg</url>
      <title>Forem: Sawit Trisirisatayawong</title>
      <link>https://forem.com/tansawit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tansawit"/>
    <language>en</language>
    <item>
      <title>How to Validate Credit Card Numbers using Luhn's Algorithm</title>
      <dc:creator>Sawit Trisirisatayawong</dc:creator>
      <pubDate>Mon, 03 Feb 2020 13:20:43 +0000</pubDate>
      <link>https://forem.com/tansawit/how-to-validate-credit-card-numbers-using-luhn-s-algorithm-5ca8</link>
      <guid>https://forem.com/tansawit/how-to-validate-credit-card-numbers-using-luhn-s-algorithm-5ca8</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2OjCkId7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/X3RPhY8/rupixen-com-Q59-Hmz-K38e-Q-unsplash.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2OjCkId7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/X3RPhY8/rupixen-com-Q59-Hmz-K38e-Q-unsplash.jpg" alt="Cover Photo"&gt;&lt;/a&gt;&lt;br&gt;
Ever looked at a credit card and wonder if the numbers on it means anything? Turns out those number are not entirely random, and instead follow a set of regular patterns. These patterns in turn allow merchants and others to not only validate the authenticity of the card, but also find other useful information about it. Here’s how they are generated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identifying the Card Issuer
&lt;/h2&gt;

&lt;p&gt;The first six digits of the card, known as the &lt;strong&gt;Issuer Identification Number (INN)&lt;/strong&gt; tell us about the who the issuer of the card is. Some example of this from the more well-known issuers are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Card Issuer&lt;/th&gt;
&lt;th&gt;Card Number Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Visa&lt;/td&gt;
&lt;td&gt;4xxxxx&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MasterCard&lt;/td&gt;
&lt;td&gt;51xxxx-55xxxx, 222100-272099*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Discover&lt;/td&gt;
&lt;td&gt;6011xx, 644xxx, 65xxxx&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;American Express (Amex)&lt;/td&gt;
&lt;td&gt;34xxxx, 37xxxx&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;or more strictly, the card numbers follow a set of &lt;a href="https://en.wikipedia.org/wiki/Regular_expression"&gt;regular expression&lt;/a&gt; patterns:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Card Issuer&lt;/th&gt;
&lt;th&gt;Card Number Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Visa&lt;/td&gt;
&lt;td&gt;^4[0-9]{12}(?:[0-9]{3})?\$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MasterCard&lt;/td&gt;
&lt;td&gt;^5[1-5][0-9]{14}\$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Discover&lt;/td&gt;
&lt;td&gt;see this &lt;a href="https://gist.github.com/michaelkeevildown/9096cd3aac9029c4e6e05588448a8841"&gt;page&lt;/a&gt; for full regex&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;American Express (Amex)&lt;/td&gt;
&lt;td&gt;^3[47][0-9]{13}\$&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For those interested, a full list of all the RegEx each issuers can be found on this &lt;a href="https://gist.github.com/michaelkeevildown/9096cd3aac9029c4e6e05588448a8841"&gt;page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As part of Mastercard’s &lt;a href="https://www.mastercard.us/en-us/issuers/get-support/2-series-bin-expansion.html"&gt;2-series expansion&lt;/a&gt; back in 2017, their cards can now begin with 2&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Validating the Card Number
&lt;/h2&gt;

&lt;p&gt;A typical string of credit card numbers can be divided into a couple of groups:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RiR2jkN5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/YBK4wFP/card-number-group.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RiR2jkN5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/YBK4wFP/card-number-group.png" alt="Credit Card Number Grouping"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Based on the above diagram&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Major Industry Identifier (MII)&lt;/strong&gt; - identifies the industry of the card. See &lt;a href="https://en.wikipedia.org/wiki/ISO/IEC_7812#Major_industry_identifier"&gt;here&lt;/a&gt; for the full list of industries and digit&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Issuer Identification Number (IIN)&lt;/strong&gt; — identifies the issuer of the card. American Express starts with 34 or 37, Mastercard starts with 2221–2720 or 51–55, Visa starts with 4. See &lt;a href="https://en.wikipedia.org/wiki/Payment_card_number#Issuer_identification_number_(IIN)"&gt;here&lt;/a&gt; for a list of all IIN ranges.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Account Number&lt;/strong&gt; — identifies the cardholder's account&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Checksum&lt;/strong&gt; — makes sure that the account number is valid&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we get to the cool bit. Luhn's Algorithm determines the validity of a card using the account number and checksum (labels 3 and 4).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;From the rightmost digit of your card number, double every digit&lt;/li&gt;
&lt;li&gt;If any digit is greater than 9 after the above doubling, add the digits of the result (i.e. 16 =&amp;gt; 1+6 = 7). Alternatively, the same result can be achieved by subtracting 9 from the that result (i.e. 16 =&amp;gt; 16-9 = 7)&lt;/li&gt;
&lt;li&gt;Sum up all of the digits&lt;/li&gt;
&lt;li&gt;If the sum ends in a zero, then the card is valid according to the Luhn formula&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example: Assume our card number is 4417 1234 5678 9113&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wn4V4ohX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/2ZY4QFn/luhns-algorithm-example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wn4V4ohX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/2ZY4QFn/luhns-algorithm-example.png" alt="Example of Luhn's Algorithm"&gt;&lt;/a&gt;&lt;br&gt;
Summing all of the digits in the last row together we get 70, which ends in a zero and thus our card is valid!&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithm Limitations
&lt;/h2&gt;

&lt;p&gt;While Luhn's algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits, it cannot detect the transposition of the sequence "(&lt;em&gt;first-valid-character&lt;/em&gt;)(&lt;em&gt;last-valid-character&lt;/em&gt;)" to "(&lt;em&gt;last-valid-character&lt;/em&gt;)(&lt;em&gt;first-valid-character&lt;/em&gt;)" or vice versa. More specifically for our 0-9 digit set, it will not detect transposition of the two-digit pair '&lt;em&gt;09&lt;/em&gt;' to '&lt;em&gt;90&lt;/em&gt;' or vice versa. The reason and proof behind this weakness is quite mathematically involved but can be found &lt;a href="https://www.academia.edu/19957955/Transposition_Error_Detection_in_Luhn_s_Algorithm"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Concluding
&lt;/h2&gt;

&lt;p&gt;Now that we understand the general process of how to validate cards and identify who the issuer is, we can automate the process using a simple script. For those who are interested in how to write one, I be posting a short tutorial using Go in the coming weeks.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>algorithms</category>
      <category>payment</category>
      <category>security</category>
    </item>
    <item>
      <title>Building a Simple REST API with Go</title>
      <dc:creator>Sawit Trisirisatayawong</dc:creator>
      <pubDate>Sat, 01 Feb 2020 03:42:11 +0000</pubDate>
      <link>https://forem.com/tansawit/building-a-simple-rest-api-with-go-356l</link>
      <guid>https://forem.com/tansawit/building-a-simple-rest-api-with-go-356l</guid>
      <description>&lt;p&gt;After recently joining &lt;a href="https://central.tech"&gt;Central Tech&lt;/a&gt;, I was given my first project: implement a new product creation and querying API in Go. I took this as a chance for me to finally dive deep into Go, something I've been wanted to do after having heard praises about it from many others. I also decided to start detailing the things I learned along the way, both for personal reference and to ensure that I understand concepts enough to explain it in writing. &lt;/p&gt;

&lt;p&gt;As such, this article and my &lt;a href="https://tansawit.me"&gt;site&lt;/a&gt; will hopefully come to act as a resource that I can refer back to, and hopefully be helpful to someone else as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics
&lt;/h2&gt;

&lt;p&gt;This piece assumes that you have go installed and working on your computer. If not, please see the Go language's official getting started &lt;a href="https://golang.org/doc/install"&gt;guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Before writing the actual functions to execute, we first need to define our program and import the necessary packages. Create a new file called &lt;code&gt;main.go&lt;/code&gt; and add the following lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's go over each of the components of the code above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Packages
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://golang.org/pkg/"&gt;Packages&lt;/a&gt; are Go's source code, organized into system directories. They allow for code reusability across different applications. In order to differentiate an executable program from a library to be used by other programs, Go dictates that we includes &lt;code&gt;package main&lt;/code&gt; in the header of the main file of any executables. When compiling, that &lt;code&gt;package main&lt;/code&gt; tells the Go compiler that the package should compile as an executable program rather than being built as a library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Imports
&lt;/h3&gt;

&lt;p&gt;The keyword 'import' is used for importing packages into our program or another package. When importing packages, the Go compiler will look in the locations specified by the two environment variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$GOROOT&lt;/code&gt; where packages from the &lt;a href="https://golang.org/pkg/"&gt;standard library&lt;/a&gt; shipped with Go are stored and&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$GOPATH&lt;/code&gt; for fetched third-party or self-made packages.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Go by design does not allow declaration of unused import and will adamantly complain about it during compile time if any is found&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In our case, both packages comes from the standard library:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;log&lt;/code&gt; allows us to log errors and other issues.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;net/http&lt;/code&gt; gives us HTTP client and server implementation for building the actual API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After declaring the packages, we can get to defining and serving our API. Without using third party libraries and routers, there are two main 'approaches' to this. &lt;/p&gt;

&lt;p&gt;If we implement our handler as a function, we'd use &lt;code&gt;http.HandleFunc&lt;/code&gt;. Otherwise, if we'd implemented our handler as a type with a &lt;code&gt;ServeHTTP&lt;/code&gt; method, we'd use &lt;code&gt;http.Handle&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For simple implementations, the first option is much easier and clearer to read and understand. But using a struct type allows storing of useful information in it, an example of which is the &lt;a href="https://golang.org/src/net/http/fs.go?s=12662:12702#L418"&gt;file server&lt;/a&gt; from the standard library. The struct contains the root directory for file service. See this &lt;a href="https://stackoverflow.com/questions/21957455/difference-between-http-handle-and-http-handlefunc"&gt;Stack Overflow post&lt;/a&gt; for a more detailed explanation.&lt;/p&gt;

&lt;p&gt;As our program is rather simple and straightforward, we'll stick to the first approach.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: HTTP handlers are processes that runs in response to a request made to a web application. For more information, see its &lt;a href="https://en.wikipedia.org/wiki/HTTP_handler"&gt;Wikipedia page&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Building a Web Server
&lt;/h2&gt;

&lt;p&gt;Now we get to defining how our API server will actually behave, and the content we're going to send as our response. Add the following lines below our imports and package declarations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message":"hello world!"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, let's run through each function.&lt;/p&gt;

&lt;h3&gt;
  
  
  apiResponse
&lt;/h3&gt;

&lt;p&gt;The function apiResponse is responsible for writing the response code and JSON. The function itself takes two arguments: &lt;code&gt;w&lt;/code&gt; of type &lt;code&gt;http.ResponseWriter&lt;/code&gt; assembles our HTTP server's responses, while &lt;code&gt;r&lt;/code&gt; reads and parse the requests. Now to go through each line of the function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;w.WriteHeaer(http.StatusOK)&lt;/code&gt; writes the HTTP &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status"&gt;response code&lt;/a&gt; 200, indicating that the requested resource (the JSON response in our case) has been successfully fetched and is transmitted in the message body.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;w.Header().Set("Content-Type", "application/json")&lt;/code&gt; indicates the return type of the requested resource.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;w.Write([]byte('{"message":"hello world!"}'))&lt;/code&gt; finally writes our response in the message body.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  main
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;main()&lt;/code&gt; function serves a special purpose in Go: it acts as the entry point of the executable programs. It does not take any argument nor return anything. And unlike some other languages, we do not need to explicitly call &lt;code&gt;main&lt;/code&gt;, as Go automatically calls the function when run.&lt;/p&gt;

&lt;p&gt;In our main function, the &lt;code&gt;HandleFunc&lt;/code&gt; call tells the HTTP package to handle all requests to the web root ("/") with our apiResponse function. It then calls &lt;code&gt;http.ListenAndServe&lt;/code&gt;, specifying that it should listen on port &lt;code&gt;8080&lt;/code&gt; on any interface.&lt;/p&gt;

&lt;p&gt;If we now run the command &lt;code&gt;go run .&lt;/code&gt; and navigate to &lt;code&gt;localhost:8080&lt;/code&gt; on our web browser, we should see something like this.&lt;/p&gt;

&lt;p&gt;We can also enter the same URL into our REST Client (i.e. &lt;a href="https://www.getpostman.com/"&gt;Postman&lt;/a&gt;, &lt;a href="https://www.insomnia.rest"&gt;Insomnia&lt;/a&gt;, &lt;a href="https://paw.cloud/"&gt;Paw&lt;/a&gt;) using the "GET" method to get same result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eg56v0I_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/bgVjQpF/simple-json-response.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eg56v0I_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/bgVjQpF/simple-json-response.png" alt="simple api response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Making it RESTful
&lt;/h2&gt;

&lt;p&gt;Now that our server can return a response, we can configure it to do different things depending on the method being requested.&lt;/p&gt;

&lt;p&gt;To do so, we can modify our &lt;code&gt;apiResponse&lt;/code&gt; function as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RespoeWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;// Set the return Content-Type as JSON like before&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c"&gt;// Change the response depending on the method being requested&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "GET method requested"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCreated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "POST method requested"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusNotFound&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "Can't find method requested"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we close the previous version of our server with &lt;code&gt;ctrl-c&lt;/code&gt; and start it once again, we should get different responses depending on the method we requested from our REST Client.&lt;/p&gt;

&lt;p&gt;Our final &lt;code&gt;main.go&lt;/code&gt; should look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RespoeWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;// Set the return Content-Type as JSON like before&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c"&gt;// Change the response depending on the method being requested&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "GET method requested"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCreated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "POST method requested"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusNotFound&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"message": "Can't find method requested"}`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;This covers the basic of building a REST API server. In later posts I plan on going over the &lt;code&gt;net/http&lt;/code&gt; library in more detail, as well as using third-party router libraries such as &lt;a href="https://github.com/gorilla/mux"&gt;'gorillia/mux'&lt;/a&gt; and &lt;a href="https://github.com/go-chi/chi"&gt;'go-chi/chi'&lt;/a&gt; for more complex routings.&lt;/p&gt;

&lt;p&gt;If you have any questions, feel free to reach out to me on &lt;a href="https://twitter.com/tansawit"&gt;Twitter&lt;/a&gt; or via &lt;a href="//mailto:sawit.tr@gmail.com"&gt;email&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>tutorial</category>
      <category>api</category>
      <category>restapi</category>
    </item>
  </channel>
</rss>
