GoLang: The Better Framework

"GoLang was reportedly built by developers when they were waiting for the code compilation to complete in a project.

Need is the mother of all invention and GoLang is no exception to this law. In an era where JavaScript and Python ruled development, developers build it as the one-stop-solution for the problems they were facing with these languages. The three main capabilities they sought-after were the ease of coding, efficient code-compilation and efficient execution. It is also one of the most wanted languages as per Stackoverflow Survey 2020. So, let’s understand how GoLang is different from Javascript and Python and quickly build an app in it to put it to the test.

comparision_GoLangArticle.png

What is Goroutine?

Goroutine has a dynamic growable call stack which starts from only 4KB and can go up to the total memory capacity, while other languages have static stack sizes, usually of 1MB (determined by the OS).

Why Revel framework?

Revel is on the top of the list of fully-featured GoLang web frameworks. It offers pre-configured features, which lead to the optimum usage, as well as in-built third-party plugins or middleware, as it is self-sufficient, unlike other Golang frameworks. For developing APIs, Revel is the one-stop-solution, regardless of the complex functions. Features of Revel:

  • Huge & Active Community.
  • No Third-Party Plugins, Middleware, Configuration or Setup.
  • Full-fledged Framework.

Let’s create an app using Revel framework:

GoLang Setup on Mac:

  • Create Go directory
$ mkdir $HOME/go
  • Setup your Go paths
    $ export GOPATH=$HOME/go 
    $ export GOROOT=/usr/local/opt/go/libexec 
    $ export PATH=$PATH:$GOPATH/bin 
    $ export PATH=$PATH:$GOROOT/bin
    
  • Install Go using Homebrew:
    $ brew install go
    

    If you do not have Homebrew, please install it from here.

Revel setup & creating a new project

  • Install Revel packages

    $ cd $GOPATH
    $ go get github.com/revel/revel
    $ go get github.com/revel/cmd/revel
    
  • Create a new Revel app

    $ revel new authapp
    
  • Run the app

    $ revel run -a authapp
    

To test if the application is running, open http://localhost:9000/ in your browser. If you can see something like the page below, then your app is running fine.

golang.png

MongoDB setup:

  • Install MongoDB using Homebrew

    $ brew tap mongodb/brew && brew install mongodb-community
    
  • Start MongoDB server

    $ brew services start mongodb-community
    
  • Check MongoDB running

    $ mongo
    

    If it is not running, try redoing the above steps again.

Our MongoDB installation is now complete. If you are facing any issues, then you may refer to this.

Install the MongoDB package for Revel.

$ cd $GOPATH
$ go get github.com/kyawmyintthein/revel_mgo

Setup MongoDB in your project.

$ cd authapp
$ revel_mgo mgo:setup

Update DB config in project

Add the below code in conf/app.conf file.

[dev] //it is already in app.conf
 mongo.database = database_name
 mongo.path = localhost
 mongo.maxPool = 20
[pro] //it is already in app.conf
 mongo.database = pro_database_name
 mongo.path = localhost
 mongo.maxPool = 20

Add initApp() in app/init.go

func initApp() {
    Config, err := revel.LoadConfig("app.conf")
    if err != nil || Config == nil {
        log.Fatalf("%+v",err)
    }
    mongodb.MaxPool = revel.Config.IntDefault("mongo.maxPool", 0)
    mongodb.PATH,_ = revel.Config.String("mongo.path")
    mongodb.DBNAME, _ = revel.Config.String("mongo.database")
    mongodb.CheckAndInitServiceConnection()
}

To call initApp() function on Revel start, add this inside the init() revel.OnAppStart(initApp)

Generate the model

To generate models you can use the revel_mgo generate command:

$ revel_mgo generate model Notes -fields=title:string,content:string

Generate the Controller

$ revel_mgo generate controller Notes

Update routes in conf/routes file

GET /notes NotesController.Index
POST /notes NotesController.Create
PUT /notes NotesController.Update
GET /notes/:id NotesController.Show
DELETE /notes/:id NotesController.Delete

Finally, run the application and test the routes in Postman.

Congratulations, you have created an application using Go.

Conclusion:

GoLang is special because:

  • It has faster compilation and execution times.
  • It is natively designed for concurrent programming.
  • It has better code readability and consistency.
  • It allows development with multiple languages.
  • It allows for easier maintenance of dependencies.

Thank you so much for reading. I hope this article helps you understand what GoLang is and gives you a reason to try it for yourself.

No Comments Yet