Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go Serving: Building Server App With Go

Go Serving: Building Server App With Go

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

Leong Hean Hong

March 19, 2016
Tweet

More Decks by Leong Hean Hong

Other Decks in Programming

Transcript

  1. 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
  2. Agenda • Highlights of Go language • What I did

    with Go • How I did it • Getting team to adopt Go
  3. Characteristics Of Go • Compiled • Statically typed • With

    garbage collection • Concurrency support • Emits self-contained executable, with minimal dependencies • Matured, stable • Active communities
  4. Variable Declaration package main import "fmt" func main() { var

    foo string foo = "Answer:" bar := 42 fmt.Println(foo, bar) }
  5. 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
  6. 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” }
  7. Pointer package main import "fmt" type Vertex struct { X

    int Y int } func main() { v := Vertex{1, 2} p := &v p.X = 123 // Alternative: (*p).X = 123 fmt.Println(v) }
  8. Struct, Method package main import "fmt" type Human struct {

    Age int // Public variable name string // Private variable } // Public function func (human *Human) Walk() { fmt.Println(human.name, "walked.") } // Private function func (human *Human) foo() { fmt.Println("Only accessible within this package.") } func main() { bob := Human{name: "Bob"} bob.Age = 42 bob.Walk() }
  9. Implicit Interface Implementation package main import "fmt" type Flyable interface

    { Fly() } type Bird struct { Name string } // Implicit interface implementation func (bird Bird) Fly() { fmt.Println(bird.Name, "is flying.") } // Implement fmt.Stringer interface func (bird Bird) String() (string) { return bird.Name } func main() { var burung Flyable = Bird{Name: "Bob"} burung.Fly() // "Bob is flying." fmt.Println(burung) // "Bob" }
  10. 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) }
  11. 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) }
  12. 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
  13. 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) }
  14. 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 }
  15. 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 }
  16. Tips • Use runtime (https://golang.org/pkg/runtime/) package for profiling • Use

    systemd for managing server app (http://bit. ly/1M517tk)
  17. 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.