Fast Web Applications with Go

50 %
50 %
Information about Fast Web Applications with Go
Technology

Published on March 9, 2014

Author: eeozekin

Source: slideshare.net

Description

A presentation that provides an introduction to Go language for building web applications.

Fast Web Applications with Go ! DevFestW Istanbul ! March 2,2014! Ekin Eylem Ozekin!

Outline ! ²  Introduction to Go! ²  Web with Go! ²  Is Go suitable to Web! ²  Demo Web Application! ²  Questions !

But First of All, Me ! •  Ekin Eylem Ozekin! •  BSc. In Computer Science, MSc. In Software Engineering! •  Currently, Ph.D. Candidate at Bogazici University, on Machine Learning! •  Works at GE !

Introduction to Go ! •  Built around 2007, announced at 2009! •  By “GO”ogle! •  Open Source! •  Similar to C! •  Statically typed, compiled! •  No pointer arithmetic !

Introduction to Go ! •  Automatic memory management, classes (structs), type inference! •  First class functions, suitable to functional programming! •  Concurrency support, channels !

Introduction to Go ! •  Ideal for fast, distributed systems! •  Can import libraries directly from URLs! •  Built-in UTF-8 support (İ, Ğ etc. works perfectly)! •  Integrated AppEngine libraries !

Introduction to Go ! 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)! } !

Introduction to Go ! •  Written as read, left from right! •  No semicolons! •  Multiple return values (think of tuples in Python)! •  Single binary file after compile! •  More on the previous code later !

Web with Go ! •  Built-in http package! •  Similar to J2EE Servlets! •  But more like a Micro-framework !

Web with Go ! •  Web Development with the performance of C! •  Might be deployed with built-in server (port listener actually)! •  Deploy to Apache or Nginx with FastCGI! •  Also mod_go for Apache! •  Deploy to AppEngine or Heroku !

Is Go Suitable for Web ! Should I use Go for Web? !

Is Go Suitable for Web ! Pros! •  •  Micro-framework style http package! •  CGI support (kinda old but solid and useful)! •  Compiled, fast performance! •  C like Syntax but better! •  API is mature enough!

Is Go Suitable for Web ! Cons! •  •  Not easier then PHP (or Ruby or Python)! •  Not as widely used as others! •  Not proven enough yet (can be argued)!

Is Go Suitable for Web ! Last Word! •  •  Service Oriented Architecture and Micro Frameworks fit well! •  You don’t have to use just Go, use with others! •  Google already uses it (isn’t it enough reason?)!

Demo ! A Web application with Go ! ! Let’s GO!

Source Code ! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Server Side Code! package main! import ("html/template"; "fmt"; "net/http"; "strconv")! func main() {! http.HandleFunc("/", form)! http.HandleFunc("/show_age", form_handler)! http.ListenAndServe(":8080", nil)! }! func form(writer http.ResponseWriter,! request *http.Request) {! genderList := []string { "Male", "Female" }! t, _ := template.ParseFiles("form.html")! t.Execute(writer, map[string]interface{} {! "genders": genderList,! "title": "DevFest Applicant Form",! })! }! func form_handler(writer http.ResponseWriter,! request *http.Request) {! gender := request.FormValue("gender")! message := ""! if ("1" == gender) {! message = "You don't ask a woman her age."! } else {! age, _ := strconv.Atoi(request.FormValue("age"))! if (age < 1000) {! message = "This boy is still alive and kicking"! } else {! message = ”Still too young!"! } ! }! fmt.Fprintf(writer, message)! } ! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Template Code! <h1>{{.title}}</h1>! <p>! Fill in your gender and age.! <br />! And I will tell you if you are old or not...! </p>! <form action="/show_age" method="POST">! <dl>! <dt>Gender</dt>! <dd>! <select name="gender" style="width: 125px;">! {{range $index, $value := .genders}}! <option value="{{$index}}" />{{$value}}! {{end}}! </select>! </dd>! <dt>Age</dt>! <dd><input type="text" name="age" /></dd>! </dl>! <input type="submit" value="Save">! </form> !

More information ! ²  http://golang.org/doc/! ²  http://golang.org/doc/articles/gos_declaration_syntax.html! ²  http://blog.iron.io/2013/08/go-after-2-years-in-production.html!

Questions? ! ? ! Thank you for listening! ! Ekin Eylem Ozekin! eeozekin@gmail.com !

Q&A Session ! •  •  •  •  •  •  •  •  Is there a Database Abstraction layer?! 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! What can I use for User Interfaces?! There is a GTK binding for that. For more libraries you can check: http://go-lang.cat-v.org/library-bindings! To return multiple values Python returns actually a Tuple data structure. Is it similar in Go too?! No. Go really returns multiple values.! If I can something on a template, should I recompile in order to see changes?! 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. !

Add a comment

Related presentations