A few months ago, I watched a video from Insinyur Online channel (NGOPREK — Belajar Kotlin Sambil Buat Web Server). There, the hosts (Giovanni Sakti & Iqbal Farabi), tried to learn Kotlin by creating a simple HTTP server. Back then, I was interested in applying this method to learn a new language, specifically Golang, since I’ll use Golang in my current job.
And two days ago, Insinyur Online held a live stream and announced a giveaway to celebrate their 1K total subscribers achievement. For those who would like to participate in the giveaway, the rules were simple. You only need to create an article about your own research or journey in learning about anything that they have taught in their live streams. And I think this is a good opportunity for me to start learning Go by creating a simple project (an HTTP server) and documenting the process within this post.
Along the way, I was also accompanied by some helpful websites. Since I haven’t learned about Go, I just check those websites when I was creating this project.
Return ‘Hello World’ With a GET Method
First thing first, I created a GitHub repository to host my code. You may access it here. And the first thing that I would like to create is, to have a GET method, that’ll return a string Hello World when we access the route.
Here, I imported some of the important packages that we need to run the webserver. Then I created a handler function, which was required by the HTTP package later on. In the handler function, I return a “Hello World” string.
Then I created the main function, where I assign the handler into our root URL with the HandleFunc function. And then we run the server using http.ListenAndServe. To run this file, we need to type
go run main.go
Then, we can go to http://localhost:8080/ to see the results of our code once we run it.
Return a List of Strings
The second thing that I would like to do is, to create an array of String and return it once it’s called. I’ll use the GET method for this part since we won’t post or gives any value to the server.
Here, I modified the code that I have created before. I changed the code inside the handler function. To print an array of Strings, we need to create the array first. I created an array that consists of 3 members. Then, I do a for-in loop and return each position & string from the array.
After I created this project, now I know how to create a simple server in Go using the net/http package. I also learn the basics of the Go programming language (i.e. how to create and assign value to a variable, for-in loop, print variable, create a function, etc).
I guess this practice helps me in escaping the “tutorial hell”. So, I won’t be stuck with reading or watching tutorials about Go, but instead, I just use the new language that I’m currently learning to build a project.
And that’s just it. I hope this post gives a valuable lesson for you, and maybe you could implement this method of learning a new programming language to life.
If you would like to check out the code, you can access it here.