Tag:thrift go
Article From:https://www.cnblogs.com/lanyangsh/p/9059894.html

thrift Originally the RPC communication framework used by Facebook, it was later contributed to the Apache foundation, which came out earlier, almost supported all the back-end languages, and was very widely used as an unknown network framework.

Like grpc, you need to define communication protocols first and then implement your business logic.

The following methods are introduced.

download the go thrift Library

$ go get git.apache.org/thrift.git/lib/go/thrift/...

Note: the repository time is 2017.7 at that time, so context was not supported at that time.

> define protocol file echo.thrift

Create a thriftExample directory in the GOPATH directory and create a file echo.thrift in the directory. The contents are as follows:

namespace go echo

struct EchoReq {
    1: string msg;

struct EchoRes {
    1: string msg;

service Echo {
    EchoRes echo(1: EchoReq req);

protocol conversion

Command format

thrift --gen <language> <Thrift filename>

As for the usage of thrift, you can use thrift -help to view help.

The following command will generate the gen-go folder, which translates the above protocol into golang code.

$ thrift -r --gen go echo.thrift 

The generated gen-go folder includes the following files

[lanyang@localhost thriftExample]$ ll gen-go/ -R
total 4
drwxrwxr-x. 3 lanyang lanyang 4096 May 13 23:18 echo

total 28
-rw-rw-r--. 1 lanyang lanyang   349 May 13 23:18 echo-consts.go
-rw-rw-r--. 1 lanyang lanyang 16161 May 13 23:18 echo.go
drwxrwxr-x. 2 lanyang lanyang  4096 May 13 23:18 echo-remote
-rw-rw-r--. 1 lanyang lanyang   159 May 13 23:18 GoUnusedProtection__.go

total 4
-rwxr-xr-x. 1 lanyang lanyang 4017 May 13 23:18 echo-remote.go

Among them, echo.go is the type and method definition generated, and echo-remote is the client sample code.

sample code”

The following is a simple example of its use.

server-side code

The server code server.go is as follows:

package main

import (




type EchoServer struct {

func (e *EchoServer) Echo(req *echo.EchoReq) (*echo.EchoRes, error) {
    fmt.Printf("message from client: %v\n", req.GetMsg())

    res := &echo.EchoRes{
        Msg: "success",

    return res, nil

func main() {
    transport, err := thrift.NewTServerSocket(":9898")
    if err != nil {

    handler := &EchoServer{}
    processor := echo.NewEchoProcessor(handler)

    transportFactory := thrift.NewTBufferedTransportFactory(8192)
    protocolFactory := thrift.NewTCompactProtocolFactory()
    server := thrift.NewTSimpleServer4(

    if err := server.Serve(); err != nil {

client code

The client code client.go is as follows

package main

import (



func main() {
    transportFactory := thrift.NewTBufferedTransportFactory(8192)
    protocolFactory := thrift.NewTCompactProtocolFactory()

    transport, err := thrift.NewTSocket(net.JoinHostPort("", "9898"))
    if err != nil {
        fmt.Fprintln(os.Stderr, "error resolving address:", err)

    useTransport, err := transportFactory.GetTransport(transport)
    client := echo.NewEchoClientFactory(useTransport, protocolFactory)
    if err := transport.Open(); err != nil {
        fmt.Fprintln(os.Stderr, "Error opening socket to", " ", err)
    defer transport.Close()

    req := &echo.EchoReq{Msg:"You are welcome."}
    res, err := client.Echo(req)
    if err != nil {
        log.Println("Echo failed:", err)

    log.Println("response:", res.Msg)
    fmt.Println("well done")


server ouput:

$ ./server
message from client: You are welcome.

client output:

$ ./client
2018/05/19 11:12:37 response: success
well done

Note: the protocol used by the server and the client must be the same, for example, the transport factory is buffer transport factroy, and protocol factory is compaCT protcol.



github go thrift

Building Apache Thrift on CentOS 6.5

How to install thrift tools for different systems

go thrift example(It is of great reference value)

golang Thrift of the network framework

GolangPerfect implementation of cross language calls through the Thrift framework

Install thrift


Link of this Article: Go thrift use examples

Leave a Reply

Your email address will not be published. Required fields are marked *