Sunday, April 24, 2016

GO with MYSQL

GO's default SQL package is database/sql, and it allows more generalized database connectivity. In this post, I'm going to use third party mysql driver called Go-MySQL-Driver. You can install it using following command.
Then we need to create a database schema for this. I'll use student table schema like below.
Create GO project using Eclipse. Then add Student struct like below.
Then we need to import required packages.
And also in the main function I have connected to my mysql database. In the connection string you have to give mysql database, username and password appropriately.
Following image show how to write the CreateStudent function.
Next I have wrote GetStudent function.
You can invoke above functions from the main function like below.

Thursday, April 7, 2016

RabbitMQ Client Server using Go

RabbitMQ is an open source message broker software that implements Advanced Message Queue Protocol(AMQP). In this demo I will implement message queuing client server architecture using GO.
First we need to install a GO library which can communicate with RabbitMQ using AMQP protocol. AMQP is one of the mostly used GO client for AMQP.  First, I will install AMQP library in my local machine.
Next implement get queue function. We could directly create queue and publish the messages. But for re-usability purposes I wrote below function. The getQueue function returns three arguments. First one is the actual connection between RabbitMQ and application. Channel is the communication path between RabbitMQ and application. Last one is the actual queue. If an error occurred you can see the logs using handleError helper function. You can check documentation for QueueDeclare function from here.
Next we need to publish messages to above queue.
Our main function should be like below.
Run the main.go file in your solution folder.
In the RabbitMQ console you will be able to see an output similar to below screenshot.
Next we will implement queue subscribe client.
Now we need to change main function accordingly.
Run main.go file and you will see following output in RabbitMQ console.