Developing Connext DDS in Golang in 4 Simple Steps

rti-blog-post-image-2019-01-10-golang-4-simple-steps-642x396-0119

Let’s get Go’ing with DDS in 2019! I am so excited to share that there is a new and quick way to develop a DDS application in Go! RTI Go Connector now provides a simplified API for easy DDS development as we’ve done with Connectors for Python and Javascript. If you have already used RTI Connector, you can quickly ramp up as it follows the same convention. If you haven’t used RTI Connector, please check out how-to tips in this blog!

Before jumping into RTI Go Connector, I would like to talk a little bit about the Go programming language. Go is an open source programming language designed for building simple, fast, and reliable software. While the syntax of Go is similar to dynamically-typed languages for ease of use, it is a statically-typed, compiled language for high performance, efficiency and safety. Go also supports built-in concurrency features to easily maximize the capabilities of multi-core networked computers. Although Go is relatively young (created in 2009), there are already many frameworks, libraries and software developed. You can find a curated project list for Go here.

With RTI Go Connector, you can easily integrate RTI Connext DDS with lots of other technologies already developed and available in Go.

The motivation for us to develop Go Connector was to integrate RTI Connext DDS with InfluxDB (the time-series database for monitoring). We could quickly develop DDS plugins for Telegraf (a plugin-driven monitoring agent for InfluxDB) using Go Connector. The size of plugin code is roughly a mere 100 lines.

To give you an idea how Go Connector API works, I would like to go over a simple writer example.

The first thing you need to do is to import the Connector package.

import "github.com/rticommunity/rticonnextdds-connector-go"

After that, you can create a connector object by calling the “NewConnector” function. To create a connector object, you have to provide an XML-Based App Creation configuration file and the name of a DDS Participant defined in the XML configuration file. Go provides an interesting primitive called defer. As it is named, a defer statement delays the execution of a function until the surrounding function returns. You can then call “Delete” function to delete the created connector object right after the NewConnector function.

connector, err := rti.NewConnector("MyParticipantLibrary::Zero", filepath)

defer connector.Delete()

You need a connector output object to write a sample. To get an output object, you need to pass the name of a DDS DataWriter defined in the XML configuration file. 

output, err := connector.GetOutput("MyPublisher::MySquareWriter")

Next, you can set fields in a sample instance. Connector provides set functions for all primitives types supported in Go.

output.Instance.SetInt("x", 1)

output.Instance.SetInt("y", 2)

output.Instance.SetInt("shapesize", 30)

output.Instance.SetString("color", "BLUE")

Finally, you can write the sample.

output.Write();

This describes a simple example of how to publish DDS data with Go Connector. Basically, you need four steps:

  1. Create a connector object
  2. Get an output object
  3. Set values to a sample
  4. Write the sample

You can also set values directly to Go structs. Go offers built-in support for JSON encoding and decoding, and Go Connector utilizes functions provided by Connext API for converting DDS to JSON vice versa to do this.

type Shape struct {

     Color string `json:"color"`

     X int `json:"x"`

     Y int `json:"y"`

     Shapesize int `json:"shapesize"`

}

 

var shape Shape

shape.Y = 2

output.Instance.Set(&shape)

If you are interested in learning more about the new Go Connector, please refer to the following GitHub. It describes how to get started and provides simple examples.

 https://github.com/rticommunity/rticonnextdds-connector-go

Let us know on our RTI Community forum if you have any questions. Happy coding!

Getting Started with Connext DDS

Connext® DDS is the world's leading implementation of the Data Distribution Service (DDS) standard for Real-Time Systems. Try a fully-functional version of Connext DDS for 30 days.


Free Trial