View
1.049
Download
0
Category
Preview:
Citation preview
Go ServingBuilding Server App With Go
Leong Hean Hong | hong@mrleong.net |
About
● Hong develops web services in Go and PHP.● Built a server for proprietary GPS trackers using Go
○ My first experience with Go○ As a way of introducing Go to the team
● This talk is for developers interested in adopting Go
Agenda
● Highlights of Go language● What I did with Go● How I did it● Getting team to adopt Go
Highlights Of Go Language
Characteristics Of Go
● Compiled● Statically typed● With garbage collection● Concurrency support● Emits self-contained executable, with minimal dependencies● Matured, stable● Active communities
Variable Declaration
package main
import "fmt"
func main() {var foo stringfoo = "Answer:"
bar := 42fmt.Println(foo, bar)
}
C Language Artifacts
package main
import "fmt"
func main() {for i := 0; i < 10; i++ {
fmt.Println("Hello world!")}
}
#include <stdio.h>
int main(void) {int i;for (i = 0; i < 10; i++) { printf("Hello world!\n");}return 0;
}
Go C
Multiple Return Values
package main
import "fmt"
func swap(a int, b int) (int, int) {return b, a
}
func main() {foo, bar := swap(123, 456)fmt.Println(foo, bar) // “456 123”
}
Pointer
package main
import "fmt"
type Vertex struct {X intY int
}
func main() {v := Vertex{1, 2}
p := &vp.X = 123 // Alternative: (*p).X = 123
fmt.Println(v)}
Struct, Method
package main
import "fmt"
type Human struct {Age int // Public variablename string // Private variable
}
// Public functionfunc (human *Human) Walk() {
fmt.Println(human.name, "walked.")}
// Private functionfunc (human *Human) foo() {
fmt.Println("Only accessible within this package.")}
func main() {bob := Human{name: "Bob"}bob.Age = 42bob.Walk()
}
Implicit Interface Implementation
package main
import "fmt"
type Flyable interface {Fly()
}
type Bird struct {Name string
}
// Implicit interface implementationfunc (bird Bird) Fly() {
fmt.Println(bird.Name, "is flying.")}
// Implement fmt.Stringer interfacefunc (bird Bird) String() (string) {
return bird.Name}
func main() {var burung Flyable = Bird{Name: "Bob"}burung.Fly() // "Bob is flying."fmt.Println(burung) // "Bob"
}
Defer
package main
import ("fmt""io/ioutil""net/http"
)
func main() {resp, err := http.Get("http://www.example.com")if err != nil {
return}defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)fmt.Printf("%s", body)
}
Goroutine
package main
import "fmt"import "time"
func say(something string) {fmt.Println(something)
}
func main() {go say("world")say("Hello")time.Sleep(100 * time.Millisecond)
}
What I Did With Go
Server For GPS Tracker
Go Server Data Processing(PHP)
3rd Party App
GPS Tracker
Server For GPS Tracker
● Communicate proprietary protocol over TCP connection● Act as proxy between GPS tracker and applications, relay information● Encode/decode data, does not process the data further● Maintain persistent connections with multiple trackers
I used to be a programmer like you, until I took an arrow in the knee
How I Did It
Listener (net package)
listener, err := net.Listen("tcp", ":7700")if err != nil {
// handle err}
for {conn, err := listener.Accept()if err != nil {
// handle err}
go HandleConnection(conn)}
Send Data (net package)
func Send(data string, conn net.Conn) (err error) {_, err = conn.Write([]byte(data))if err != nil {
// handle err}return err
}
Receive Data
for {data, err := bufio.NewReader(conn).ReadString('\n')if err != nil {
// handle err}fmt.Print(data)
}
Read Config File
● INI-style config file (sections, and key-value pairs)● Create a wrapper around github.com/robfig/config to add support for default
values
c, _ = config.ReadDefault(somefile.cfg')func GetInt(section string, option string, default int) {
value, err = c.Int(section, option)if err != nil {
return default}
return value}
Tips
● Use runtime (https://golang.org/pkg/runtime/) package for profiling
● Use systemd for managing server app (http://bit.ly/1M517tk)
Getting Team To Adopt Go
● Start from creating simple, easy-to-implement, tools/microservice● Share your learnings with team● Share articles of Go usage in famous companies● Lead by example, you create something useful first● Create a culture that embraces experimentations and constant
learning● Force them to use it. Have to start using it to appreciate it.● Go has strength & weakness. Use it appropriately, not for the sake
of using it.
So long, and thanks for all the fish
Recommended