E. Eylem Ozekin
Computer scientist (PhD Candidate) with a sense of humor, entrepreneur, techie, l'enfant terrible, passionate dreamer, absolute bookworm with a really cool day job
GOing Forward
Fast Web Applications with Go Lang
GDG DevFest - Istanbul
Dec 6th, 2014
Ekin Eylem Ozekin

Background
BSc. in Computer Science, MSc. in Software Eng.
Experience
OS projects, freelance, banking, TA, Aviation...
Interests
Software, technology & web

Performance of Statically-typed languages
Usability of
Dynamic programming languages
+
go get github.com/foo/bar
import "github.com/baz/qux"
Github, Bitbucket, Google Code & Launchpad are supported
package main
import (
  "fmt"
  "net/http"
)
func get_name() (string, string) {
  // No reason to break a few rules, right
  var hello = "Hello "
  audience := "DevFestTR"
  return hello, audience
}
func handler(writer http.ResponseWriter, request *http.Request) {
  hello, audience := get_name()
  fmt.Fprintf(writer, hello + audience)
}
func main() {
  http.HandleFunc("/", handler)
  http.ListenAndServe(":8080", nil)
}
More on this later...
Written as read left to right
No semicolonsMultiple return value (think of tuples in Python)
Single binary file after compile
Concurrency
Runtime
Pass by Value
Native & Fast
Type Systems
GO
(or Ruby or Python)
(yet can be argued)
New... Fun..
Dropbox
SoundCloud
BBC
MongoDB
    1 package main
    2 import ("html/template"; "fmt"; "net/http"; "strconv")
    3 func main() {
    4   http.HandleFunc("/", form)
    5   http.HandleFunc("/show_age", form_handler)
    6   http.ListenAndServe(":8080", nil)
    7 }
    8 func form(writer http.ResponseWriter,
    9           request *http.Request) {
   10   genderList := []string { "Male", "Female" }
   11   t, _ := template.ParseFiles("form.html")
   12   t.Execute(writer, map[string]interface{} {
   13     "genders": genderList,
   14     "title": "DevFest Applicant Form",
   15   })
   16 }
   17 func form_handler(writer http.ResponseWriter,
   18                   request *http.Request) {
   19   gender := request.FormValue("gender")
   20   message := ""
   21   if ("1" == gender) {
   22     message = "You don't ask a woman her age."
   23   } else {
   24     age, _ := strconv.Atoi(request.FormValue("age"))
   25     if (age < 1000) {
   26       message = "This boy is still alive and kicking"
   27     } else {
   28       message = ”Still too young!"
   29     }  
   30   }
   31   fmt.Fprintf(writer, message)
    1 <h1>{{.title}}</h1>
    2 <p>
    3   Fill in your gender and age.
    4   <br />
    5   And I will tell you if you are old or not...
    6 </p>
    7 <form action="/show_age" method="POST">
    8   <dl>
    9     <dt>Gender</dt>
   10     <dd>
   11       <select name="gender" style="width: 125px;">
   12         {{range $index, $value := .genders}}
   13           <option value="{{$index}}" />{{$value}}
   14         {{end}}
   15       </select>
   16     </dd>
   17     <dt>Age</dt>
   18     <dd><input type="text" name="age" /></dd>
   19   </dl>
   20   <input type="submitCODE EXAMPLES
package main
import ("net/http"; "io/ioutil"; "encoding/json"; "strings"; "fmt")
func main() {
    response, _ := http.Get("http://ipinfo.io/json")
    defer response.Body.Close()
    body, _ := ioutil.ReadAll(response.Body)
    type IPInfo struct {
        Ip, Hostname, City, Region, Country, Loc, Org string
    }
    var ipInfo IPInfo
    dec := json.NewDecoder(strings.NewReader(string(body[:])))
    dec.Decode(&ipInfo)
    fmt.Printf("%s %s %s\n", ipInfo.Ip, ipInfo.City, ipInfo.Country)
}
String transformation
standard lib:encoding/json
package main
import("database/sql"; _ "github.com/go-sql-driver/mysql"; "log"; "fmt")
func main() {
    db, err := sql.Open("mysql", "root:lessecret@tcp(127.0.0.1:3306)/les_dernier_combat")
    if err != nil {
        log.Fatal(err)
    }   
    defer db.Close()
    var ( id int; name string; rank string ) 
    rows, err := db.Query("select id, name, rank from les_combatants where id < ?", 13) 
    if err != nil {
        log.Fatal(err)
    }   
    defer rows.Close()
    for rows.Next() {
        rows.Scan(&id, &name, &rank)
        fmt.Printf("%v: %v, %v\n", id, name, rank)
    }   
}
Defer pushes a function call onto list
Saved calls are executed after function returns
package main
import ("net/http"; "encoding/json")
func showHeroes(writer http.ResponseWriter, request *http.Request) {
    type SuperheroGroup struct {
        ID     int 
        GroupName   string
        Members []string
    }   
    group := SuperheroGroup{
        ID:     1,  
        GroupName:   "Fantastic Four",
        Members: []string{"Reed", "Sue", "Johnny", "Ben"},
    }   
    response, _ := json.Marshal(group)
    writer.Write(response)
}
func main() {
    http.HandleFunc("/heroes", showHeroes)
    http.ListenAndServe(":1234", nil)
}request/response parameters
writing the response body
func main() {
    m := martini.Classic()
    m.Get("/", func() string {
      return "mvp" // TODO: Have an actual MVP
    })
    m.Run()
}$ gin
[gin] listening on port 3000
[martini] listening on :3001 (development)Have some martini
Have some gin, too...
Hack away without interruptions...
Binding to the / endpoint
Restarts the application on code changes (Martini)
As there are no classes ORM is a bit crippled. However, there are few implementations on that. You can find those here:
http://jmoiron.net/blog/golang-orms/
There are also bindings for popular databases:
http://go-lang.cat-v.org/library-bindings
There is a GTK binding for that. For more libraries you can check: http://go-lang.cat-v.org/library-bindings
No.
GO really returns multiple values :)
No, you don't.
If you make a change on the template code, all you have to do is refresh the page.
However, you should be careful, when deploying to the server you should make sure that those templates exist too.
http://www.gorillatoolkit.org/
Still have questions?
email: eeozekin@gmail.com
* Pun intended
By E. Eylem Ozekin
More on Go, programming & web.
Computer scientist (PhD Candidate) with a sense of humor, entrepreneur, techie, l'enfant terrible, passionate dreamer, absolute bookworm with a really cool day job