Table of Contents
- Introduction
- Getting Ready to Go
- Installation
- Creating a New Go Hessian
- Using the GoHessian Serializer
1. Introduction and How to Use This Guide
GoHessian is simple to use, and can quickly be deployed for use in a varity of situations, ranging from basic encoding and decoding to integration into different projects.
This manual will take a look into GoHessian's Go API and general usage.
In our examples, we will assume a few things:
- If a variable is not shown as declared, it is assumed that it has already been declared earlier
- If imports are not shown, it is assumed they have already been imported
- HOST_CONNECT_URL refers to a host connection URL string. Ex. "localhost:7100"
- Go is sometimes refered to, both in this guide and beyond, as Golang. They are the same thing, both refering to the Go Programming language.
2. Getting Ready To Go
To use GoHessian, you'll need Go. Setting up your Go enviroment is very simple.
- First, ensure you have successful installed Go, which can be done here.
- Next, ensure you're GOPATH enviromental variable is properly set. Generally speaking, you'll want GOPATH to specify the location of your Go workspace. For a more detailed explanation of GOPATH, see this page.
- This page provides an excellent introduction to the Go language and Go code.
3. Installation
Getting GoHessian is easy. Simply open your terminal, and run the command go get github.com/viant/gohessian
. This will fetch GoHessian from our github, and load it into your Go source, where you can use it!
4. Creating a New Instance of GoHessian
Now that your Go enviroment is ready to go, you can begin to use GoHessian. The first step to using GoHessian is to create a new instance of GoHessian. First, import GoHessian into your program by using the following code:
import(
"github.com/viant/gohessian"
)
You may find it helpful to declare an alias to reference GoHessian in the code, by doing this:
import(
hessian "github.com/viant/gohessian"
)
From this point on, this usage guide will be written as if an alias was declared. Now that you have imported GoHessian, you are free to use its methods in your own code. The first step to doing this is creating a new instance of GoHessian. This is done as such:
gh := hessian.NewGoHessian(nil,nil)
This creates a new instance of GoHessian named gh. Passing nil to the function creates new maps as the parameters. If you wish to use your own maps, pass them instead of nil. gh, the value returned by NewGoHessian, is an interface of type Serializer. This interface contains two methods, ToBytes and ToObject, the two principle methods that serialization uses.
5. Using the GoHessian Serializer
There are two important methods in the serializer--ToBytes and ToObject--that carry out opposite tasks, which are encoding and decoding the data, respectively. Passing an object to ToBytes will produce a byte array, and passing the byte array to ToObject will produce the original object. Assume we create a struct as such:
type Person struct {
firstName string
lastName string
}
Now, we'll create a Person object and encode, then decode, the object:
person := Person{"John","Doe"}
gh := NewGoHessian(nil, nil)
byteArray, _ := gh.ToBytes(p)
same_person, _ := gh.ToObject(bt)
Here, person
is an instance of struct Person, gh
is our GoHessian instance, and byteArray
is the value of the Person after it has been encoded. To decode the Person, the byteArray
is passed to ToObject
, which returns an object that has the same value as person
.