Introduction to Web Development Apps in Go Programming
Welcome to my next blog, fellow go programmers. In this blog, we will be talking about Google’s awesome Golang language. In my previous blog, you might have seen how Golang has evolved over the years and has given tough competition to Java and C++. But in this blog, we would be talking about its real-world applications. Golang is Google’s official programming language.
Google has kept its license Open source. It means that anyone can modify, copy, paste or change it as per their needs. Golang is a high-level programming language. However, Go has a lot of characteristics similar to C or Java; it’s hardly anything like either C or Java.
In fact, Go Programming was intended to be a language, which could be a combination of Python, C and Java. Even though it can never replace these languages, but still other languages cannot do what Go can, and to speak the truth, it’s actually much faster than C or Python. Enough about how GO programming works now; let us see the real-world applications of it besides being of a lot of help to Google and how it has actually dominated the world of web development.
Through out this blog, I will be assuming that you have some basic knowledge in programming C, Java or atleast Django. If not, you can read my other blogs to understand it and then come back here and continue with this one.
The Go Programming Language Example
Following is an example:
Origin of GO
Remember I told you that Go received its characteristics from web development languages like C and python, but the truth is, it actually borrowed a lot than just characteristics. It has the agility of the compilation speed of python without losing the safe polarity of C. The miniature builds of GO are spot-on; for example, you can compile large programs in just seconds. The speeds of these bytecodes are almost similar to that of the C’s.
The main reason GO Programming was developed because Google had very large data critical servers and the programmers invested, or the better term would be wasted, long time waiting for the programs to compile. Even though the code was compiled and parallelized, it still took a very long time to build a single program.
Even incremental builds were slow (Incremental builds means just updating old builds with new features or cleansing its bugs). It was then they realized that they need something different, something with the power of C and the speed of python. They also decided the tools used in these basic systems language were slow.
So they wanted to start something from scratch, something to write those kinds of programs that they needed to write at Google in a way that the builds could be really smart and short without losing their efficiency.
Web development, concurrencies, and GO
Now the thing is, how does web development work with GO? How does it gain from its concurrency? The thing is, theoretically, with parallel processing, the server’s resources could be put to better use. For example, if you run 2 independent sql queries in parallel, it will give a quick response. Isn’t this amazing?
Now, let us take a look at this the other way round.
Normally when you hear the word concurrency, you start to think that you can work on multiple threads at once. But we are not extreme professionals for our piece of code to be perfect. So, a more precise way to put this would be that instead of doing multiple tasks, you could actually end up just mangling things around you.
So, unfortunately, this develops a lot of hiccups on our way, which is actually not good because concurrency can lead to a better visual arrangement and clearer code. In short, it can be relied on for one reason, which you wouldn’t want: low-performance. But, inspite of all this, we are actually ignoring the main part. If we actually have a good set of hardware or faster computing techniques to be more precise, GO would actually work faster in a multi-core processor environment.
Now let’s take a look at how GO is different from other languages. We need to do step one: pick any global mutable state that you wish to change, then step two: implement locking. Now, this is a combination of two proper steps and two wrong things. Developers that have decent experience would agree with me that a global mutable state is a bad thing.
As a matter of fact, many coders try to remove this in the best way possible. So the utmost fact that you have step one looks like some refactoring is in order, to begin with. Step two, on the other hand, i.e. locking, is capable of achieving its goal, but at the same time, it introduces gigantic amounts of boilerplate that is extremely hard to write it down in a proper manner and then debug it correctly.
Thus, such kind of languages might have one http request reading a specific variable and another writing it. Which happened when? Is it important enough? Does it need an order to reads and writes? Does your code have this kinda logic? If so, why?
The Way to GO
The threads of GO are not what you might be used to when writing these kernels. These are actually somewhat similar to the processes of Erlang. They are extremely lightweight & both have similar goals. This does not say that GO and Erlang are the same since they have many differences of their own.
Concurrency and the channels; both go together hand-in-hand in GO programming. It can, however, be said that these channels have the real horsepower to make our automation work. And because of this nature, they prevent the routines in GO from being duplicated. Nuf said, now you can run your codes without the help of locks and mutexes. If you have the time worth googling, you will find many people trying the same methods in the form of UNIX pipes.
Building native GO Apps
Now that we know how GO works let us look at building some basic Applications in GO. Let us go through the Pre-requisites first:
- Download the go installer from the official go website (you can get it by searching download golang)
- Set the GOPATH (This is the most tricky part if you have never set environment variables in your life)
a. For Windows Users
set GOROOT=C:\go
set GOPATH=C:\Users\testdir
set GOBIN=%GOPATH%\bin
set PATH=%PATH%;c:\go\bin;%GOBIN%
cd %GOPATH%\src
In the testdir option above, set the directory you want to use (name it whatever you want) as the working directory. This will set all the directories, and when you type in the last cd (change firectory) command, it should take you to the default working directory, i.e. in our case, it’s testdir. If it does, it means it works.
b. For Linux Users
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Following are the required packages that you would need to download (these are optional, depends upon what you need to build):
You can install (or update) these packages by running the following command in your console:
go get -u <import_path>
For example, if you want to install Negroni, then you can use the following command:
go get -u github.com/codehub/negroni
For me, building web applications mean building Http servers. Http or Hypertext Transfer Protocol is a protocol that was originally built to transport only user-specific HTML documents from a specific server to a client-side web browser. As of today, Http is used to transport more than just plain texts.
I won’t be getting in deep; actually, you can refer to github where you can find more details about this project.
Now to get you started, let us begin by creating a new project in our GOPATH
cd GOPATH/src
mkdir testserver
cd testserver
Now, we can create a main.go by typing:
package main
import "net/http"
func main() {
}
Now since everything is set up, all we need to do is to import the http package, and then it will work. And now, its time to write our test server code:
http.ListenAndServe(“:2964”, http.FileServer(http.Dir(“.”)))
The http.ListenAndServer is a function used to run the server. It will run on the address given to it, i.e. port 2964 in this case, and when it receives a response, it will transfer it to the http.handler that we supplied as the second argument, which again, in this case, is the built-in http-FileServer.
We created the http.Handler with the http.FileServer will act as a server for an entire directory of files and automatically respond with the file that needs to be served on the request path. As for this piece of code we ran above, we told the server to respond with the current working directory, i.e. http.Dir(“.”)
The whole Program will then look like this:
package main
import "net/http"
func main() {
http.ListenAndServe(":2964", http.FileServer(http.Dir(".")))
}
Now to execute and make our fileserver live, we can run it whenever we want by typing:
go build
./testserver
And, now if we open this in our browser- localhost:2964/main.go or http://127.0.0.1:2964/main.go, we should be able to see the packages inside our main.go file in our web browser. The best part is, we can run this program from anywhere from within our computer and serve that directory as the basic homepage for the localhost. All of this is possible in just one line of Go programming.
Speaking of which, you should actually check out the web applications developed in github, and you will be actually astonished to see what all apps people have developed with GO programming. GO language is more than just your regular programming. Once you get the hang of this language, it is highly unlikely that you will actually go to back to your regular C, C++ or Java. So, that will be it as for now. Stay tuned for more on GO programming.
Recommended Articles
We hope that this EDUCBA information on “go programming” was beneficial to you. You can view EDUCBA’s recommended articles for more information.