2. 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
3. Agenda
● Highlights of Go language
● What I did with Go
● How I did it
● Getting team to adopt Go
5. Characteristics Of Go
● Compiled
● Statically typed
● With garbage collection
● Concurrency support
● Emits self-contained executable, with minimal dependencies
● Matured, stable
● Active communities
7. 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
8. 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”
}
9. 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)
}
10. 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()
}
11. 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"
}
16. Server For GPS Tracker
Go Server
Data Processing
(PHP)
3rd
Party App
GPS Tracker
17. 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
18. I used to be a programmer like
you, until I took an arrow in the
knee
22. Receive Data
for {
data, err := bufio.NewReader(conn).ReadString('n')
if err != nil {
// handle err
}
fmt.Print(data)
}
23. 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
}
24. Tips
● Use runtime (https://golang.org/pkg/runtime/) package
for profiling
● Use systemd for managing server app (http://bit.
ly/1M517tk)
25. 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.