A weekend with Go lang, BeeGo & React

Standard

So I’ve decided this weekend to take Go language for a ride.

I have started with Go’s awesome tour to understand what it is all about. To be honest it is quite different from my daily Ruby, yet cool enough to get me in.

Why Go and not Node.js?

the $1 million question, right?
I have never written anything big in Node, nor in Go so I am not familiar with real-life problems. I have done some mini projects to figure out what Node is all about and I love Node’s ecosystem. My friends from mean.io have done some amazing stuff with Node.
My 2 cents answer to the question: “I am afraid of Node’s callback nightmare” + I want to learn something new + some cool projects @work are in Go (openshift) to name one.

So, once I’ve comlpeted the tutorial, I have decided on my weekend project: A blog!
I mean, we never have enough of how to create blogging system 🙂

Next, I started looking for a go web framework. This is a bad practice because you can build everything in Go. from my minimal understanding those frameworks are mainly wrappers to existing libraries in Go.
There are quite a few Go frameworks: Beego, Martini, Gorilla, Revel & GoCraft to name a few. However, more experienced Go developers will tend towards Go’s Net/HTTP.
After some reading (and because I like honey), I chose Beego which seemed the easier choice for a newbie.

Hello 

Installing in Go is quite simple, you just run:

go get github.com/astaxie/beego
go get github.com/astaxie/bee (cli)

And now you’ve go two options to start a new project:
> bee new myproject – to create a new MVC ( where the view is HTML)
Or
> bee api myproject  – to create a new MVC API (so the view is JSON)

I chose the API project, as I wanted to use React as my frontend.
I have deleted all the demo controllers & models and started from scratch.

The Model:
It took me sometime to get to this model, and I am sure I can make it better (contributions welcome). And so I have crafted struggled my way to create a CRUD API which displays, creates, updates and deletes Posts. The Controller:

Finally, I had an API responding to “/v1/posts”.

Next was React.
I have followed their fine tutorial and moved it from comments (the tutorial) to Posts.
The React app looks like:

The final outcome is at: https://github.com/shlomizadok/beego_react
(fork, learn, make better 🙂 )

Eventually, I have pushed it to Heroku so others can try.
https://fathomless-taiga-3095.herokuapp.com/
Try it (and write something nice…)

 

6 thoughts on “A weekend with Go lang, BeeGo & React

  1. Lincxx

    This is really awesome. I love the article, straight to the point, with a little side humor. Now I need to learn more about Go, so I can understand the code better

  2. James

    I prefer revel, even thou its not complete, i find it more consistant and more eash ro understand. I mixed it with gorp.

    Ive trouble trying o figure how to run a one time job or task. Logging is not consistent, and database.conf cannot be shared with migration.

    I find gorp easier to catch on n lesser code compared to the resulting scaffold on beego.

    U can c my libraries under my github.com/u007

    • shlomizadok

      No doubt Revel is an excellent Golang framework. Maybe I should investigate and write about it more 🙂
      Also, if you’d like to write a guest post about gorp and what you did with revel-gorp, it would be awesome 🙂

      • james

        hi shlomizadok,

        i will try to put up my sample up for you.
        currently it uses conf/db.conf
        and ive put up access logging and added a format update that ive put a pull request to the revel github.

        in my app/init.go
        var DBMap *gorp.DbMap

        func InitDB() {
        var err error
        DBMap, err = revel_gorp.InitDatabase()
        if (err != nil) {
        revel.ERROR.Printf(“LoadDatabase error: %s”, err.Error())
        }

        DBMap.AddTableWithName(models.User{}, “users”).SetKeys(true, “Id”)
        err = DBMap.CreateTablesIfNotExists()
        if err != nil {
        revel.ERROR.Printf(“Create tables error: %s”, err.Error())
        }
        }

        i think u should get those login based commenting system on your comment 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *