1package main 2 3/* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22import ( 23 "context" 24 "fmt" 25 26 "github.com/apache/thrift/lib/go/thrift" 27 "github.com/apache/thrift/tutorial/go/gen-go/tutorial" 28) 29 30var defaultCtx = context.Background() 31 32func 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 81func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool, cfg *thrift.TConfiguration) error { 82 var transport thrift.TTransport 83 if secure { 84 transport = thrift.NewTSSLSocketConf(addr, cfg) 85 } else { 86 transport = thrift.NewTSocketConf(addr, cfg) 87 } 88 transport, err := transportFactory.GetTransport(transport) 89 if err != nil { 90 return err 91 } 92 defer transport.Close() 93 if err := transport.Open(); err != nil { 94 return err 95 } 96 iprot := protocolFactory.GetProtocol(transport) 97 oprot := protocolFactory.GetProtocol(transport) 98 return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot))) 99} 100