RPC in Go using Twitch’s Twirp

Gurleen Sethi
TheDeveloperCafe
Published in
2 min readNov 13, 2022

>> Read the complete article on 👉 TheDeveloperCafe <<

Getting Started

Twirp is an RPC framework from Twitch that just like gRPC uses Protobufs and is much simpler. In this article, I am going to give you a taste of Twirp. You can find the complete code for this article in this GitHub repository.

🙋‍♂️ This article presumes that you are familiar with Protobufs and the concept of RPC.

Setup Project

Project creation and installation instructions for protoc tool and twirp/proto.

  1. Create a new Go project.
$ mkdir go-twirp-rpc-example
$ cd go-twirp-rpc-example
$ go mod init github.com/gurleensethi/go-twirp-rpc-example

Create three folders server, client and rpc in the project so that the folder structure looks like this.

go-twirp-rpc-example/
├─ client/
├─ server/
├─ rpc/
├─ notes/
  1. Install the protoc and twirp-protoc generators (these are cli tools).
$ go install github.com/twitchtv/twirp/protoc-gen-twirp@latest
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  1. Install protobuf and twirp modules in project.
$ go get github.com/twitchtv/twirp
$ go get google.golang.org/protobuf

Writing Proto file

Since Twirp uses Protobufs to generate clients, we need to write a .proto file.

You will be building a simple note-taking application that only has two features:

1. Get all notes.
2. Create a new note.

Here is the proto definition model (store it in rpc/notes/service.proto).

syntax = "proto3";
package gotwirprpcexample.rpc.notes;
option go_package = "rpc/notes";
message Note {
int32 id = 1;
string text = 2;
int64 created_at = 3;
}
message CreateNoteParams {
string text = 1;
}
message GetAllNotesParams {
}
message GetAllNotesResult {
repeated Note notes = 1;
}
service NotesService {
rpc CreateNote(CreateNoteParams) returns (Note);
rpc GetAllNotes(GetAllNotesParams) returns (GetAllNotesResult);
}

>> Read the complete article on 👉 TheDeveloperCafe <<

--

--