把thrift上关于go的例子跑通
地址:http://thrift.apache.org/tutorial/go
1. 需要将thrift安装目录里lib/下的go部分拷贝到$GOPATH(如果发现没有的话)
# import "git.apache.org/thrift.git/lib/go/thrift"
mkdir -p $GOPATH/src/git.apache.org/thrift.git/lib/
cp -r thrift/lib/go $GOPATH/git.apache.org/thrift.git/lib/go
2. copy官方thrift教学文件tutorial.thrift、shared.thrift
文档地址: http://thrift.apache.org/tutorial/go
源代码里有: thrift/tutorial/go/src
源代码里看这个文件可以了解如何跑教学例子: thrift/tutorial/go/Makefile.am
tutorial.thrift: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/tutorial.thrift
shared.thrift: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/shared.thrift
thrift -r --gen go tutorial.thrift
#会生成gen-go,目录下两个文件夹shared、tutorial
#复制这俩目录到$GOPATH,主要为了可以导入就行,具体目录随意,这里放到my.learn
cp -r shared/ $GOPATH/src/my.learn/shared
cp -r tutorial/ $GOPATH/src/my.learn/tutorial
#从官方网站下载测试代码
https://git1-us-west.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/go/src/client.go;hb=HEAD
https://git1-us-west.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/go/src/handler.go;hb=HEAD
https://git1-us-west.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/go/src/main.go;hb=HEAD
https://git1-us-west.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/go/src/server.go;hb=HEAD
#这里把shared、tutorial放到my.learn下了,需要修改部分引用
#创建可执行程序
go build
#开启服务
# -server表示开启server模式,代码里还有其他参数,一看便知
./test -server
#server返回:
*thrift.TServerSocket
Starting the simple server... on localhost:9090
ping()
add(1,1)
calculate(1, {DIVIDE,1,0})
ping()
add(1,1)
calculate(1, {DIVIDE,1,0})
#客户端测试调用
./test
#client返回:
ping()
1+1=2
Invalid operation: InvalidOperation({WhatOp:4 Why:Cannot divide by 0})
error running client: InvalidOperation({WhatOp:4 Why:Cannot divide by 0})
#其实在my.learn/tutorial/calculator-remote里有个客户端测试代码:
go build
./calculator-remote add 1 2
#返回值(上面的server不要关)
3 <nil>
#在my.learn/shared/shared_service-remote里也有个客户端测试代码
go build
./shared_service-remote getStruct 4
#返回值
<nil> <nil>
网友评论