THRIFT-2012 Modernizing Go

Patch: Travis Cline
diff --git a/tutorial/go/src/CalculatorHandler.go b/tutorial/go/src/CalculatorHandler.go
deleted file mode 100644
index 9eb2838..0000000
--- a/tutorial/go/src/CalculatorHandler.go
+++ /dev/null
@@ -1,101 +0,0 @@
-package main;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import (
-  "fmt"
-  "os"
-  "strconv"
-  "thriftlib/tutorial"
-  "thriftlib/shared"
-)
-
-type CalculatorHandler struct {
-  log map[int]*shared.SharedStruct
-}
-
-func NewCalculatorHandler() *CalculatorHandler {
-  return &CalculatorHandler{log:make(map[int]*shared.SharedStruct)}
-}
-
-func (p *CalculatorHandler) Ping() (err os.Error) {
-  fmt.Print("ping()\n")
-  return nil
-}
-
-func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err os.Error) {
-  fmt.Print("add(", num1, ",", num2, ")\n")
-  return num1 + num2, nil
-}
-
-func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, ouch *tutorial.InvalidOperation, err os.Error) {
-  fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
-  switch(w.Op) {
-  case tutorial.ADD:
-    val = w.Num1 + w.Num2
-    break
-  case tutorial.SUBTRACT:
-    val = w.Num1 - w.Num2
-    break
-  case tutorial.MULTIPLY:
-    val = w.Num1 * w.Num2
-    break
-  case tutorial.DIVIDE:
-    if w.Num2 == 0 {
-      ouch = tutorial.NewInvalidOperation()
-      ouch.What = int32(w.Op)
-      ouch.Why = "Cannot divide by 0"
-      return
-    }
-    val = w.Num1 / w.Num2
-    break
-  default:
-    ouch = tutorial.NewInvalidOperation()
-    ouch.What = int32(w.Op)
-    ouch.Why = "Unknown operation"
-    return
-  }
-  entry := shared.NewSharedStruct()
-  entry.Key = logid
-  entry.Value = strconv.Itoa(int(val))
-  k := int(logid)
-  /*
-  oldvalue, exists := p.log[k]
-  if exists {
-    fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
-  } else {
-    fmt.Print("Adding ", entry, " for key ", k, "\n")
-  }
-  */
-  p.log[k] = entry, true
-  return val, ouch, err
-}
-
-func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, os.Error) {
-  fmt.Print("getStruct(", key, ")\n")
-  v, _ := p.log[int(key)]
-  return v, nil
-}
-
-func (p *CalculatorHandler) Zip() (err os.Error) {
-  fmt.Print("zip()\n")
-  return nil
-}
-
diff --git a/tutorial/go/src/GoClient.go b/tutorial/go/src/GoClient.go
deleted file mode 100644
index 9440530..0000000
--- a/tutorial/go/src/GoClient.go
+++ /dev/null
@@ -1,92 +0,0 @@
-package main;
-
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import (
-  "fmt"
-  "net"
-  "os"
-  "thrift"
-  "thriftlib/tutorial"
-)
-
-func Perform(client *tutorial.CalculatorClient) (err os.Error) {
-  client.Ping()
-  fmt.Print("ping()\n")
-  
-  sum, _ := client.Add(1, 1)
-  fmt.Print("1+1=", sum, "\n")
-  
-  work := tutorial.NewWork()
-  work.Op = tutorial.DIVIDE
-  work.Num1 = 1
-  work.Num2 = 0
-  quotient, ouch, err := client.Calculate(1, work)
-  if err != nil {
-    fmt.Print("Error during operation: ", err.String(), "\n")
-    return err
-  } else if ouch != nil {
-    fmt.Print("Invalid operation: ", ouch.String(), "\n")
-  } else {
-    fmt.Print("Whoa we can divide by 0 with new value: ", quotient, "\n")
-  }
-  
-  work.Op = tutorial.SUBTRACT
-  work.Num1 = 15
-  work.Num2 = 10
-  diff, ouch, err := client.Calculate(1, work)
-  if err != nil {
-    fmt.Print("Error during operation: ", err.String(), "\n")
-    return err
-  } else if ouch != nil {
-    fmt.Print("Invalid operation: ", ouch.String(), "\n")
-  } else {
-    fmt.Print("15-10=", diff, "\n")
-  }
-  
-  log, err := client.GetStruct(1)
-  if err != nil {
-    fmt.Print("Unable to get struct: ", err.String(), "\n")
-    return err
-  } else {
-    fmt.Print("Check log: ", log.Value, "\n")
-  }
-  return err
-}
-
-
-func RunClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) os.Error {
-  addr, err := net.ResolveTCPAddr("localhost:9090")
-  if err != nil {
-    fmt.Print("Error resolving address: ", err.String(), "\n")
-    return err
-  }
-  transport := thrift.NewTSocketAddr(addr)
-  if err = transport.Open(); err != nil {
-    fmt.Print("Error opening connection for protocol ", addr.Network(), " to ", addr.String(), ": ", err.String(), "\n")
-    return err
-  }
-  useTransport := transportFactory.GetTransport(transport)
-  client := tutorial.NewCalculatorClientFactory(useTransport, protocolFactory)
-  Perform(client)
-  return transport.Close()
-}
diff --git a/tutorial/go/src/GoServer.go b/tutorial/go/src/GoServer.go
deleted file mode 100644
index f70a2a9..0000000
--- a/tutorial/go/src/GoServer.go
+++ /dev/null
@@ -1,85 +0,0 @@
-package main;
-
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import (
-  "fmt"
-  "net"
-  "thrift"
-  "thriftlib/tutorial"
-)
-
-
-type GoServer struct {
-  handler tutorial.ICalculator
-  processor *tutorial.CalculatorProcessor
-}
-
-func NewGoServer() *GoServer {
-  handler := NewCalculatorHandler()
-  processor := tutorial.NewCalculatorProcessor(handler)
-  return &GoServer{handler:handler, processor:processor}
-}
-
-func Simple(processor *tutorial.CalculatorProcessor, transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, ch chan int) {
-  addr, err := net.ResolveTCPAddr("localhost:9090")
-  if err != nil {
-    fmt.Print("Error resolving address: ", err.String(), "\n")
-    return
-  }
-  serverTransport, err := thrift.NewTServerSocketAddr(addr)
-  if err != nil {
-    fmt.Print("Error creating server socket: ", err.String(), "\n")
-    return
-  }
-  server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
-  // Use this for a multithreaded server
-  // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
-  
-  fmt.Print("Starting the simple server... on ", addr, "\n")
-  for {
-    err = server.Serve()
-    if err != nil {
-      fmt.Print("Error during simple server: ", err.String(), "\n")
-      return
-    }
-  }
-  fmt.Print("Done with the simple server\n")
-  ch <- 1
-}
-
-func Secure(processor *tutorial.CalculatorProcessor) {
-  addr, _ := net.ResolveTCPAddr("localhost:9091")
-  serverTransport, _ := thrift.NewTNonblockingServerSocketAddr(addr)
-  server := thrift.NewTSimpleServer2(processor, serverTransport)
-  fmt.Print("Starting the secure server... on ", addr, "\n")
-  server.Serve()
-  fmt.Print("Done with the secure server\n")
-}
-
-func RunServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) {
-  server := NewGoServer()
-  ch := make(chan int)
-  go Simple(server.processor, transportFactory, protocolFactory, ch)
-  //go Secure(server.processor)
-  _ = <- ch
-}
diff --git a/tutorial/go/src/Makefile b/tutorial/go/src/Makefile
deleted file mode 100644
index 87acbca..0000000
--- a/tutorial/go/src/Makefile
+++ /dev/null
@@ -1,22 +0,0 @@
-# Copyright 2009 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-
-include $(GOROOT)/src/Make.inc
-
-all: install
-
-TARG=TutorialServerClient
-
-DIRS=\
-
-GOFILES=\
-  CalculatorHandler.go\
-  GoClient.go\
-  GoServer.go\
-  main.go
-
--include ../Make.deps
-
-include $(GOROOT)/src/Make.cmd
-
diff --git a/tutorial/go/src/client.go b/tutorial/go/src/client.go
new file mode 100644
index 0000000..114de19
--- /dev/null
+++ b/tutorial/go/src/client.go
@@ -0,0 +1,85 @@
+package main
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"fmt"
+	"git.apache.org/thrift.git/lib/go/thrift"
+	"tutorial"
+)
+
+func handleClient(client *tutorial.CalculatorClient) (err error) {
+	client.Ping()
+	fmt.Println("ping()")
+
+	sum, _ := client.Add(1, 1)
+	fmt.Print("1+1=", sum, "\n")
+
+	work := tutorial.NewWork()
+	work.Op = tutorial.Operation_DIVIDE
+	work.Num1 = 1
+	work.Num2 = 0
+	quotient, ouch, err := client.Calculate(1, work)
+	if err != nil {
+		fmt.Println("Error during operation:", err)
+		return err
+	} else if ouch != nil {
+		fmt.Println("Invalid operation:", ouch)
+	} else {
+		fmt.Println("Whoa we can divide by 0 with new value:", quotient)
+	}
+
+	work.Op = tutorial.Operation_SUBTRACT
+	work.Num1 = 15
+	work.Num2 = 10
+	diff, ouch, err := client.Calculate(1, work)
+	if err != nil {
+		fmt.Println("Error during operation:", err)
+		return err
+	} else if ouch != nil {
+		fmt.Println("Invalid operation:", ouch)
+	} else {
+		fmt.Print("15-10=", diff, "\n")
+	}
+
+	log, err := client.GetStruct(1)
+	if err != nil {
+		fmt.Println("Unable to get struct:", err)
+		return err
+	} else {
+		fmt.Println("Check log:", log.Value)
+	}
+	return err
+}
+
+func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) error {
+	var transport thrift.TTransport
+	transport, err := thrift.NewTSocket("localhost:9090")
+	if err != nil {
+		fmt.Println("Error opening socket:", err)
+		return err
+	}
+	transport = transportFactory.GetTransport(transport)
+	defer transport.Close()
+	if err := transport.Open(); err != nil {
+		return err
+	}
+	return handleClient(tutorial.NewCalculatorClientFactory(transport, protocolFactory))
+}
diff --git a/tutorial/go/src/handler.go b/tutorial/go/src/handler.go
new file mode 100644
index 0000000..3d4c18c
--- /dev/null
+++ b/tutorial/go/src/handler.go
@@ -0,0 +1,99 @@
+package main
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"fmt"
+	"shared"
+	"strconv"
+	"tutorial"
+)
+
+type CalculatorHandler struct {
+	log map[int]*shared.SharedStruct
+}
+
+func NewCalculatorHandler() *CalculatorHandler {
+	return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
+}
+
+func (p *CalculatorHandler) Ping() (err error) {
+	fmt.Print("ping()\n")
+	return nil
+}
+
+func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err error) {
+	fmt.Print("add(", num1, ",", num2, ")\n")
+	return num1 + num2, nil
+}
+
+func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, ouch *tutorial.InvalidOperation, err error) {
+	fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
+	switch w.Op {
+	case tutorial.Operation_ADD:
+		val = w.Num1 + w.Num2
+		break
+	case tutorial.Operation_SUBTRACT:
+		val = w.Num1 - w.Num2
+		break
+	case tutorial.Operation_MULTIPLY:
+		val = w.Num1 * w.Num2
+		break
+	case tutorial.Operation_DIVIDE:
+		if w.Num2 == 0 {
+			ouch = tutorial.NewInvalidOperation()
+			ouch.What = int32(w.Op)
+			ouch.Why = "Cannot divide by 0"
+			return
+		}
+		val = w.Num1 / w.Num2
+		break
+	default:
+		ouch = tutorial.NewInvalidOperation()
+		ouch.What = int32(w.Op)
+		ouch.Why = "Unknown operation"
+		return
+	}
+	entry := shared.NewSharedStruct()
+	entry.Key = logid
+	entry.Value = strconv.Itoa(int(val))
+	k := int(logid)
+	/*
+	   oldvalue, exists := p.log[k]
+	   if exists {
+	     fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
+	   } else {
+	     fmt.Print("Adding ", entry, " for key ", k, "\n")
+	   }
+	*/
+	p.log[k] = entry
+	return val, ouch, err
+}
+
+func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, error) {
+	fmt.Print("getStruct(", key, ")\n")
+	v, _ := p.log[int(key)]
+	return v, nil
+}
+
+func (p *CalculatorHandler) Zip() (err error) {
+	fmt.Print("zip()\n")
+	return nil
+}
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)
+		}
+	}
 }
diff --git a/tutorial/go/src/server.go b/tutorial/go/src/server.go
new file mode 100644
index 0000000..929e223
--- /dev/null
+++ b/tutorial/go/src/server.go
@@ -0,0 +1,39 @@
+package main
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"fmt"
+	"git.apache.org/thrift.git/lib/go/thrift"
+	"tutorial"
+)
+
+func runServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory) error {
+	transport, err := thrift.NewTServerSocket("localhost:9090")
+	if err != nil {
+		return err
+	}
+	handler := NewCalculatorHandler()
+	processor := tutorial.NewCalculatorProcessor(handler)
+	server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)
+
+	fmt.Println("Starting the simple server... on ", transport.Addr())
+	return server.Serve()
+}