I tried a build a simple HTTP Server in GoLang

Introduction

Hi this is Akshay Rao currently working in Annotation.Inc. I wanted to learn Go language, so thought of building a simple project. In this project ,I have built a simple a HTTP server which gives a html Response in the web browser when the paths are changed.

Let's Start

Download a goLang from https://go.dev/ according to your Operating System.

I have used VS code as code editor.

  • We will have to specify the package, using main package because it instructs the Go compiler to build the package as an executable application rather than a shared library. Our executable application will launch from the "main" function in the package.
  • Create a file name it as .go extension like I have named as “project.go”.
  • GoLang has packages for all the commands, so we will have to import packages
.
    1. a. fmt- will help in printing out the results and getting inputs from users.
      
b. html - to print the html response on the web browser.
      
c. log- if there is any error while running the server it will log the error in terminal.

      d. net/http - to create a server.
  • Create a function main.
  • Call the HandlerFunc from the http api, which will call the function specified in it fro the specified path.
  • At last we will run the server with the help of ListenAndServe at 8080 and set the error message as nil.
  • Wrap the ListenAndServe in log.Fatal to the show the error if it occurs.
  • Then run “go run /folder name/.go”
  • Open localhost:8080 in your web browser
  • Add /home to the url
  • Conclusion

    I will try to host it on docker with the help of docker file.
    Thank you