GTU Project training in Ahmedabad

Is Go (Golang) Programming Language?

Author Image Icon

Hiren

Last Updated: 28 Oct 2024



Yes, the Go is an open source programming language which is developed by GOOGLE and is designed to target users who are seeking the simple and consistent software system.

Go will offer you a type system (A collection of rules that assigns a property called a type of the various constructs like a variable, expressions, modules that a computer program is composed of.) with a modern look in its design. It is a strongly typed language with an object oriented type of programming, but it does not have any type of hierarchy, if you want to achieve it, you can only achieve through the interface.

What pros you will find in Go (Golang) are:
  • Quick Compilation time
  • Concurrent language offers simplicity
  • Functions itself are first class objects
  • Go holds garbage collections
  • In built strings and maps
  • Go compile to machine code
What cons you will find in Go (Golang) are:

  • It is an experimental language may have changed in it.
  • Windows OS is not much used to work on the Go
  • You will find lack of some things in a library, for an example: UI toolkit
  • Go doesn’t support generics.
  • It is not an object oriented in from some facet.

How Go be Different from the others?:

  • If you will go through the language specification details, then you will find many of the things which differ from other object-oriented learn programming languages. I have studied language specification of Go and what I have derived from it is as below.
  • There is no any overloading of methods and operators like oops.
  • There is no type of inheritance to import values of another class.
  • Concurrent primitives are built in that makes it outstanding.
  • You can share memory in Go by communicating only, you won’t find any other way to share memory.
  • You will find built in maps in Go to use.
Code Organization in Go programming language
The Go tool builds up the source packages are stored in the bin and pkg directories in the form of directories. Those directories hierarchy is as follows:

src: Have Go source files in the form of packages.
pkg: Have a package objects code
bin: have executable code

Here I have provided one simple web application that is created by using Go programming language.

This example is about knowing and understanding purpose. So in the first attempt we will build a static site and then we will look forward towards improvement to convert it into dynamic.

Below is the code structure of the static site, you don’t need to create this repo structure as this is already placed under the workspace. This is only for an example to know the structure.
Server Data Security

Now let we see how to serve an HTML file placed in the public directory.
Server Data Security

The index.html file holds the following code

Server Data Security

The below GO program is written to serve files statically which is residing in the public folder.

Server Data Security
You can run above program using the simple code as follows:
Go run gostatic.go
Improved Solution
Above code was a basic Go programming that does not apparently separate the routing and serving concern. Here below is an improved code to accomplish that.

Server Data Security
How the program flows:

ListenAndServe -> Strats HTTP server at the Mentioned TCP address and then calls Serve with handler.
Handler – > Nil , so default router DefaultServerMux take over.

In DefaultServerMux,
ServerMuxes : Router -> It’s a HTTP router, compares incoming requests against the defined list and then calls the Associated handler.
Handler-> handles the request, I.e : responding to the appropriate headers, body etc.

You can run the above code same as you have done for the first code to see the output.

Dynamic Sites
If you have run your program successfully, Lets go for the dynamic example of the Go programming, Let us have a look at the programming which adding the timestamp after every refresh of your web page. After every reloading you will get different output.

Server Data Security
Program flow:
Fmt -> That works with printing function , Implements formatted input/output functions like a C’s stdio library.
Time-> imported from the standard package and use it to respond back to the response writer object.
HandleFunc -> registers the URL path with the handler function into the DefaultServeMux
http.HandlerFunc -> takes http.ResponseWriter and http.Request as its arguments, who are familiar with Servlets |( JAVA) must know this.
http.ResponseWriter -> this object can send a response to the client.
r.URL.path-> to access the URL path

Now run this code and access the local host on port: 8080, you will find the current time is changing after every refresh.
The above was the example which was showing the system time in the output, there was not any input-output process dynamically. So, Here I am giving you one example, which accepts a username on the index page and then when your form is submitted then, it greets the user on the next page.

Following is the code to input the user’s data into the public directory.

When you input data on the above form it will redirect you to the greet.html , so let we write the code for greet.html which will render when the request comes .

Here is the code for that,

Server Data Security
The Go code is as follows:

Server Data Security
In the above code I have used an html/template to keep the HTML code in templates and using them to deliver on the request. But you may have questions in your mind about { {.} } coding in the greeter.html file as well as same you will find in the above coding.

The Html.template package assumes that plain text is always produced which adds escaping wherever needed to create a string with the right context. When the data value is not plain text at that time we can make sure that it is not

Escaped by specifying its type.

So basically Hey, { { . } } ! Can invoke using tmpl.Execute ( out, HTML(‘ Nishant ’)) to create Hey, Nishant!

Are you looking to gain valuable skills and knowledge? Then you are in luck! TOPS Technologies offers training courses that will be relevant to your needs. Explore our courses today, and see why we're the leading school for future success.


Stay Connected