THRIFT-2012 Modernizing Go
Patch: Travis Cline
diff --git a/tutorial/go/src/main.go b/tutorial/go/src/main.go
index 30b5d52..4b9576e 100644
--- a/tutorial/go/src/main.go
+++ b/tutorial/go/src/main.go
@@ -1,5 +1,4 @@
-package main;
-
+package main
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -20,67 +19,54 @@
* under the License.
*/
-
import (
- "flag"
- "fmt"
- "os"
- "thrift"
+ "flag"
+ "fmt"
+ "git.apache.org/thrift.git/lib/go/thrift"
+ "os"
)
func Usage() {
- fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], " <--server | --client>:\n")
- flag.PrintDefaults()
- fmt.Fprint(os.Stderr, "\n")
- os.Exit(0)
+ fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
+ flag.PrintDefaults()
+ fmt.Fprint(os.Stderr, "\n")
}
func main() {
- flag.Usage = Usage
- var client bool
- var server bool
- var protocol string
- var framed bool
- var useHttp bool
- var help bool
-
- flag.BoolVar(&client, "client", false, "Run client")
- flag.BoolVar(&server, "server", false, "Run server")
- flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson)")
- flag.BoolVar(&framed, "framed", false, "Use framed transport")
- flag.BoolVar(&useHttp, "http", false, "Use http")
- flag.BoolVar(&help, "help", false, "See usage string")
- flag.Parse()
- if help || (client && server) || !(client || server) {
- fmt.Print("flag.NArg() == ", flag.NArg(), "\n")
- flag.Usage()
- }
- var protocolFactory thrift.TProtocolFactory
- switch protocol {
- case "compact":
- protocolFactory = thrift.NewTCompactProtocolFactory()
- case "simplejson":
- protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
- case "json":
- protocolFactory = thrift.NewTJSONProtocolFactory()
- case "binary", "":
- protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
- default:
- fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
- Usage()
- os.Exit(1)
- }
- transportFactory := thrift.NewTTransportFactory()
- if framed {
- transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
- }
-
- if(client) {
- RunClient(transportFactory, protocolFactory)
- } else if(server) {
- RunServer(transportFactory, protocolFactory)
- } else {
- flag.Usage()
- }
- os.Exit(0)
+ flag.Usage = Usage
+ server := flag.Bool("server", false, "Run server")
+ protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, simplejson)")
+ framed := flag.Bool("framed", false, "Use framed transport")
+
+ flag.Parse()
+
+ var protocolFactory thrift.TProtocolFactory
+ switch *protocol {
+ case "compact":
+ protocolFactory = thrift.NewTCompactProtocolFactory()
+ case "simplejson":
+ protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
+ case "json":
+ protocolFactory = thrift.NewTJSONProtocolFactory()
+ case "binary", "":
+ protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+ default:
+ fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
+ Usage()
+ os.Exit(1)
+ }
+ transportFactory := thrift.NewTTransportFactory()
+ if *framed {
+ transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
+ }
+
+ if *server {
+ if err := runServer(transportFactory, protocolFactory); err != nil {
+ fmt.Println("error running server:", err)
+ }
+ } else {
+ if err := runClient(transportFactory, protocolFactory); err != nil {
+ fmt.Println("error running client:", err)
+ }
+ }
}