Yet Another Liferay SDK: Introducing the Liferay Go SDK

Hi everyone and Merry, Merry Christmas to all of you! smiley

In the last couple of months I had a lot of fun playing with Go, a very promising compiled, concurrent, garbage-collected, cross-platform language by Google. I thought it would have been nice to use my newly acquired skills to build something useful for the Liferay community, and so, after my Liferay SDK for Windows, I spent a few weeks in my spare time creating... guess what? wink

The Liferay Go SDK!

Just like all the other Liferay SDKs, it is a library which lets you to consume the JSON web services exposed by Liferay and your plugins, this time using the Go language. And, besides of that, it has an awesome logo designed by my friend Pier! Thank you so much! yes

Being an accurate port of the official SDKs, the Liferay Go SDK supports the same set of features, such as batch and asynchronous requests, non-primitive and binary arguments, and so on. Anyway, it was built just for fun, and so, even if it's covered by the same exact unit tests of the Android version, it's not really tested in production. Use it at your own risk! ...no, seriously, it should work! cheeky

Here is a quick example on how to use the Liferay Go SDK:

package main

import (
   "log"

   "github.com/ithildir/liferay-sdk-go/liferay"
   "github.com/ithildir/liferay-sdk-go/liferay/service/v62/blogsentry"
)

func main() {
   session := liferay.NewSession("http://localhost:8080", "test@liferay.com", "test")

   service := blogsentry.NewService(session)

   entries, err := service.GetGroupEntries(10184, 0, 10)

   if err != nil {
      log.Fatal(err)
   }

   for _, e := range entries {
      e := e.(map[string]interface{})

      log.Println(e["title"])
   }
}

As you can see, calling a Liferay service is absolutely straightforward. And, thanks to goroutines, it's easy to make asynchronous requests:

entriesChan := make(chan []interface{})

go func() {
   entries, err := service.GetGroupEntries(10184, 0, 10)

   if err != nil {
      log.Fatal(err)
   }

   entriesChan <- entries
}()

entries := <-entriesChan

for _, e := range entries {
   e := e.(map[string]interface{})

   log.Println(e["title"])
}

The source code of the SDK and the Java builder (which you can use to generate a new SDK for a different version of Liferay or for a plugin of your choice) is available on GitHub, and can be downloaded using the go get command:

go get github.com/ithildir/liferay-sdk-go/liferay

I hope this little Christmas present from me will be useful in your projects, and let me know if you find bugs, if you have any questions, etc. Any feedback is totally appreciated! smiley

Happy holidays to everybody!

1
Blogs