Sunday, August 5, 2018

Kategori: , , ,

Create a REST service using Go Language and BeeGo Framework.

beego framework go orm

Go is an Open Source programming language developed by Google Inc.
To install go latest:

Unzip in /usr/local directory:

sudo tar -C /usr/local -xzf go1.9.1.linux-amd64.tar.gz
tar -C ./ -xzf go1.9.1.linux-amd64.tar.gz
Then, modify /etc/profile file, add this line:

export PATH=$PATH:/usr/local/go/bin
also, add these lines in /home/$USER/.profile file:

export GOPATH=$HOME/golangProjects
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Installing some libraries or dependencies for Golang
Web Frameworks


BeeGo is a Web Framework to develop using Golang Web Applications or RESTful services, you can find more information pressing here.
You can install on local writting this on terminal:

go get
go get
Then, move the golangProjects/bin/bee binary to go/bin directory.

REST Service

REST(REpresentational State Transfer) is an architecture that use HTTP protocol which provide an API with methods like GET, POST, PUT, DELETE, etc.
We use BeeGo framework and mgo driver to create a REST service on Golang using MongoDB:
  • First, once installed go and beego framework binaries, creates a directory and write in shell:
    bee api myapi
    The tree directory is like:
  • Second, you should download the database drivers, in my case is MongoDB.

DataBases Drivers


Is the MongoDB Driver for Golang. For more information or Doc press here.
To install, write on terminal:
go get
go get
  • Third, install mongoDB, you can see my example pressing here or see official doc here.
    I use a Singleton Pattern to connect to database, like this:
        type singletonMongoDBSession struct {
          session *mgo.Session
          errDial error
        var instance *singletonMongoDBSession
        var once sync.Once
        func GetSessErrMongoDBSession(dialInfo string) (*mgo.Session, error){
          var instance *singletonMongoDBSession
          if dialInfo == "Dial"{
            instance = NewMongoDBSession()
          } else if dialInfo == "DialWithInfo"{
            instance = NewMongoDBSessionInfo()
          return instance.session, instance.errDial
        func NewMongoDBSession() (*singletonMongoDBSession) {
          once.Do(func() {
            sess, err := mgo.Dial(urldb)
            instance = &singletonMongoDBSession{session: sess, errDial: err}
          return instance
    Then, my model is like:
        type Login struct{
          Username           string       `json:"username"`
          Password           string       `json:"password"`
        var login Login
        func init(){}
        func VerifyLogin(object Login) (ObjectId bool, err error) {
          objlogin := loginRequest(object)
            if objlogin == (Login{}) {
            fmt.Printf("\nNo se encontro Usuario !\n\n")
            return false, nil
          } else {
            return true, nil
    I modify some files and add directories to make more understandable how you can separates service or information.
    You can see my restapi-BeeGo example, just pressing there :D .

Another Packages for develop in Golang

Communitacion Protocols

MQTT Paho Golang

Is the MQTT library for Golang. For more information press here.
To install, write on terminal:
go get
To configure Mosquitto service, you can check my tuto pressing here.

Mail Service


Is the SMTP library for Golang. For more information press here.
To install, write on terminal:
go get
Source :


Author & Editor

Suka sekali menulis dan mempelajari source code open source lisensi GNU di Github dll. Bisa pemrogramman web karena sering belajar dari contoh program yang ada di Github dll.


Post a Comment


News Letters

Subscribe here to get our newsletter in your inbox, it is safe and EASY!

IDwebgit Indonesia