Go serving: Building server app with go

26
Go Serving Building Server App With Go Leong Hean Hong | [email protected] |

Transcript of Go serving: Building server app with go

Page 1: Go serving: Building server app with go

Go ServingBuilding Server App With Go

Leong Hean Hong | [email protected] |

Page 2: Go serving: Building server app with go

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

Page 3: Go serving: Building server app with go

Agenda

● Highlights of Go language● What I did with Go● How I did it● Getting team to adopt Go

Page 4: Go serving: Building server app with go

Highlights Of Go Language

Page 5: Go serving: Building server app with go

Characteristics Of Go

● Compiled● Statically typed● With garbage collection● Concurrency support● Emits self-contained executable, with minimal dependencies● Matured, stable● Active communities

Page 6: Go serving: Building server app with go

Variable Declaration

package main

import "fmt"

func main() {var foo stringfoo = "Answer:"

bar := 42fmt.Println(foo, bar)

}

Page 7: Go serving: Building server app with go

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

Page 8: Go serving: Building server app with go

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”

}

Page 9: Go serving: Building server app with go

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)}

Page 10: Go serving: Building server app with go

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()

}

Page 11: Go serving: Building server app with go

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"

}

Page 12: Go serving: Building server app with go

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)

}

Page 13: Go serving: Building server app with go

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)

}

Page 14: Go serving: Building server app with go

Visit https://tour.golang.org/list for more

Page 15: Go serving: Building server app with go

What I Did With Go

Page 16: Go serving: Building server app with go

Server For GPS Tracker

Go Server Data Processing(PHP)

3rd Party App

GPS Tracker

Page 17: Go serving: Building server app with go

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

Page 18: Go serving: Building server app with go

I used to be a programmer like you, until I took an arrow in the knee

Page 19: Go serving: Building server app with go

How I Did It

Page 20: Go serving: Building server app with go

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)}

Page 21: Go serving: Building server app with go

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

}

Page 22: Go serving: Building server app with go

Receive Data

for {data, err := bufio.NewReader(conn).ReadString('\n')if err != nil {

// handle err}fmt.Print(data)

}

Page 23: Go serving: Building server app with go

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}

Page 24: Go serving: Building server app with go

Tips

● Use runtime (https://golang.org/pkg/runtime/) package for profiling

● Use systemd for managing server app (http://bit.ly/1M517tk)

Page 25: Go serving: Building server app with go

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.

Page 26: Go serving: Building server app with go

So long, and thanks for all the fish