micro/examples/client/codegen/codegen.go

80 lines
1.5 KiB
Go
Raw Normal View History

2015-12-03 01:44:01 +03:00
package main
import (
"fmt"
"github.com/micro/go-micro/cmd"
example "github.com/micro/go-micro/examples/server/proto/example"
"golang.org/x/net/context"
)
var (
cl = example.NewExampleClient("go.micro.srv.example", nil)
2015-12-03 01:44:01 +03:00
)
func call(i int) {
rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"})
if err != nil {
fmt.Println("call err: ", err, rsp)
return
}
fmt.Println("Call:", i, "rsp:", rsp.Msg)
}
2015-12-19 00:51:51 +03:00
func stream(i int) {
stream, err := cl.Stream(context.Background(), &example.StreamingRequest{Count: int64(i)})
2015-12-03 01:44:01 +03:00
if err != nil {
fmt.Println("err:", err)
return
}
2015-12-19 00:51:51 +03:00
for j := 0; j < i; j++ {
rsp, err := stream.Recv()
2015-12-03 01:44:01 +03:00
if err != nil {
fmt.Println("err:", err)
break
}
fmt.Println("Stream: rsp:", rsp.Count)
}
if err := stream.Close(); err != nil {
fmt.Println("stream close err:", err)
}
}
2015-12-19 00:51:51 +03:00
func pingPong(i int) {
stream, err := cl.PingPong(context.Background())
if err != nil {
fmt.Println("err:", err)
return
}
for j := 0; j < i; j++ {
if err := stream.Send(&example.Ping{Stroke: int64(j)}); err != nil {
2015-12-19 00:51:51 +03:00
fmt.Println("err:", err)
return
}
rsp, err := stream.Recv()
2015-12-19 00:51:51 +03:00
if err != nil {
fmt.Println("recv err", err)
break
}
fmt.Printf("Sent ping %v got pong %v\n", j, rsp.Stroke)
}
if err := stream.Close(); err != nil {
fmt.Println("stream close err:", err)
}
}
2015-12-03 01:44:01 +03:00
func main() {
cmd.Init()
fmt.Println("\n--- Call example ---\n")
for i := 0; i < 10; i++ {
call(i)
}
2015-12-19 00:51:51 +03:00
2015-12-03 01:44:01 +03:00
fmt.Println("\n--- Streamer example ---\n")
2015-12-19 00:51:51 +03:00
stream(10)
fmt.Println("\n--- Ping Pong example ---\n")
pingPong(10)
2015-12-03 01:44:01 +03:00
}