robo/cmd/rclient/main.go
stephen mcquay 69d2c50dc5 Fixed bug where zero value was not being used
if you read gob docs it states the following:

" If a field has the zero value for its type, it is omitted from the
transmission."

That's powerful savings. I didn't know. Now it works as one would expect.
2015-02-04 21:41:00 -08:00

68 lines
1.1 KiB
Go

package main
import (
"bufio"
"encoding/gob"
"fmt"
"net"
"os"
"strconv"
"strings"
"mcquay.me/robo"
)
const usage = "rsend <hostname>"
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
var err error
var x, y float64
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", os.Args[1], robo.Port))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
enc := gob.NewEncoder(conn)
s := bufio.NewScanner(os.Stdin)
point := robo.Point{}
for s.Scan() {
txt := s.Text()
parts := strings.Split(txt, ",")
if len(parts) != 2 {
err = fmt.Errorf("bad parse from stdin: %q", txt)
break
}
x, err = strconv.ParseFloat(parts[0], 32)
if err != nil {
break
}
y, err = strconv.ParseFloat(parts[1], 32)
if err != nil {
break
}
point.X, point.Y = x, y
fmt.Printf("sending: %+v\n", point)
err = enc.Encode(point)
if err != nil {
break
}
}
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if err := s.Err(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}