This is an example project that shows how to set up and use protocol buffers and RPC with Go.
There are three folders under src
:
counters
is where the library code lives, including the generated protoc code.counters_server
is the server componentcounters_client
is a CLI for interacting with the server.
To install the base protocol buffers compiler:
cd /tmp
curl https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.bz2 > protobuf-2.5.0.tar.bz2
tar -xf protobuf-2.5.0.tar.bz2
cd protobuf-2.5.0
./configure
make
sudo make install
NOTE: This has only been tested on OSX, but should work with any Unix that has GCC installed.
To install the Go generator for protoc:
go get -v -u github.com/kylelemons/go-rpcgen/protoc-gen-go
And we're done!
Note that you'll have to do this step before this code will build.
protoc --go_out=src/counters counters.proto
It should return silently if nothing bad happened!
Pretty straight forward:
go build counters_client
go build counters_server
If your GOPATH
isn't set yet:
export GOPATH=`pwd`
./counters_server
$ ./counters_client inc "Hello, World"
Hello, World: 1
I'd really love to hear your thoughts. How can I make this more idiomatic? Are there better patterns for this or that? Here's how you can help:
- Fork this repo.
- Commit any fixes/changes/etc.
- If you are so compelled, send a pull request.
- Send me an email, if you want: brad.heller@gmail.com