| 30 | var defaultCtx = context.Background() |
| 31 | |
| 32 | func handleClient(client *tutorial.CalculatorClient) (err error) { |
| 33 | client.Ping(defaultCtx) |
| 34 | fmt.Println("ping()") |
| 35 | |
| 36 | sum, _ := client.Add(defaultCtx, 1, 1) |
| 37 | fmt.Print("1+1=", sum, "\n") |
| 38 | |
| 39 | work := tutorial.NewWork() |
| 40 | work.Op = tutorial.Operation_DIVIDE |
| 41 | work.Num1 = 1 |
| 42 | work.Num2 = 0 |
| 43 | quotient, err := client.Calculate(defaultCtx, 1, work) |
| 44 | if err != nil { |
| 45 | switch v := err.(type) { |
| 46 | case *tutorial.InvalidOperation: |
| 47 | fmt.Println("Invalid operation:", v) |
| 48 | default: |
| 49 | fmt.Println("Error during operation:", err) |
| 50 | } |
| 51 | } else { |
| 52 | fmt.Println("Whoa we can divide by 0 with new value:", quotient) |
| 53 | } |
| 54 | |
| 55 | work.Op = tutorial.Operation_SUBTRACT |
| 56 | work.Num1 = 15 |
| 57 | work.Num2 = 10 |
| 58 | diff, err := client.Calculate(defaultCtx, 1, work) |
| 59 | if err != nil { |
| 60 | switch v := err.(type) { |
| 61 | case *tutorial.InvalidOperation: |
| 62 | fmt.Println("Invalid operation:", v) |
| 63 | default: |
| 64 | fmt.Println("Error during operation:", err) |
| 65 | } |
| 66 | return err |
| 67 | } else { |
| 68 | fmt.Print("15-10=", diff, "\n") |
| 69 | } |
| 70 | |
| 71 | log, err := client.GetStruct(defaultCtx, 1) |
| 72 | if err != nil { |
| 73 | fmt.Println("Unable to get struct:", err) |
| 74 | return err |
| 75 | } else { |
| 76 | fmt.Println("Check log:", log.Value) |
| 77 | } |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool, cfg *thrift.TConfiguration) error { |
| 82 | var transport thrift.TTransport |