SlideShare a Scribd company logo
How I built my first web 
app in Go 
16 Sep 2014 
Audrey Lim 
audreylh@gmail.com 
Twitter: @AudreyLim77
About Me 
Building Go applications
About Me 
Lawyer (1-2 yrs)
About Me 
Lawyer (1-2 yrs) 
Learning Journey 
May – Jul 2014: HTML, CSS, JS
About Me 
Lawyer (1-2 yrs) 
Learning Journey 
May – Jul 2014: HTML, CSS, JS 
(codeacademy, books, 
websites)
About Me 
Lawyer (1-2 yrs) 
Learning Journey 
(codeacademy, books, 
websites) 
May – Jul 2014: HTML, CSS, JS 
mid-Jul 2014: Go (1st backend)
About Me 
Lawyer (1-2 yrs) 
Learning Journey 
(codeacademy, books, 
websites) 
(everywhere) 
May – Jul 2014: HTML, CSS, JS 
mid-Jul 2014: Go (1st backend)
About Me 
Lawyer (1-2 yrs) 
Learning Journey 
(codeacademy, books, 
websites) 
(everywhere) 
May – Jul 2014: HTML, CSS, JS 
mid-Jul 2014: Go (1st backend) 
mid-Aug 2014: deployed 1st app – CRUD app
About Me 
Lawyer (1-2 yrs) 
Learning Journey 
(codeacademy, books, 
websites) 
(everywhere) 
May – Jul 2014: HTML, CSS, JS 
mid-Jul 2014: Go (1st backend) 
mid-Aug 2014: deployed 1st app – CRUD app 
end-Aug 2014: deployed 2nd app – API client
Go Resources 
golang.org 
Books and websites 
Sample applications 
Stackoverflow, Go-nuts mailing list
Go Resources 
golang.org (Go Tour, Docs, Go blog) 
Books and websites 
Sample applications 
Stackoverflow, Go-nuts mailing list
Go Resources 
golang.org (Go Tour, Docs, Go blog) 
Books and websites (www.golang-book.com, www.gobyexample.com) 
Sample applications 
Stackoverflow, Go-nuts mailing list
Go Resources 
golang.org (Go Tour, Docs, Go blog) 
Books and websites (www.golang-book.com, www.gobyexample.com) 
Sample applications (Go Wiki, from books) 
Stackoverflow, Go-nuts mailing list
Experience with Go
Building Go applications
CRUD App 
Simple CRUD App using MySQL 
for data persistence 
API Client 
Flickr, Weather API
1st app: CRUD App 
Sign up/Sign in 
Read Status 
Create Status 
Delete Status 
Edit Status 
Sign out 
MySQL
Basic Structure 
package main 
Import ( 
“fmt” 
“net/http” 
) 
func handler(w http.ResponseWriter, r *http.Request) { 
fmt.Fprint(w, “hello world!”) 
} 
func main() { 
http.HandleFunc(“/”, handler) 
http.ListenAndServe(“:8080”, nil) 
}
Basic Structure 
package main 
Import ( 
“fmt” 
“net/http” 
) 
All executable commands use package main 
Print output to client 
func handler(w http.ResponseWriter, r *http.Request) { 
fmt.Fprint(w, “hello world!”) 
} 
func main() { 
Handler function when root is 
requested 
http.HandleFunc(“/”, handler) 
http.ListenAndServe(“:8080”, nil) 
} 
Local server connection 
Go’s stdlib 
Entry point
Libraries 
import ( 
"database/sql" 
"html/template" 
"fmt" 
"log" 
"net/http" 
"os" 
_ "github.com/go-sql-driver/mysql" 
"github.com/gorilla/mux" 
"github.com/gorilla/securecookie” 
)
Libraries 
Standard library 
import ( 
"database/sql" 
"html/template" 
"fmt" 
"log" 
"net/http" 
"os" 
_ "github.com/go-sql-driver/mysql" 
"github.com/gorilla/mux" 
"github.com/gorilla/securecookie” 
) 
External packages
Data Structs 
Type Slice of Pointers to Post struct 
type User struct { 
Userid int64 
Username string 
Password string 
Posts []*Post 
} 
type Post struct { 
Tweetid string 
Username string 
Status string 
}
Database functions 
func ReadStatus() (res [][]string) { Return slice of slice of strings 
err := db.Ping() 
if err != nil { 
log.Fatal(err) 
} 
rows, err := db.Query("SELECT id, tweet, username FROM posts WHERE 
username = ? ORDER BY id DESC", currentuser) 
if err != nil { 
log.Fatal(err) 
} 
defer rows.Close() 
var tweet, id, username string 
for rows.Next() { 
err := rows.Scan(&id, &tweet, &username) 
if err != nil { 
return res 
} 
var a []string 
a = append(a, id, tweet, username) 
res = append(res, a) 
} 
return 
}
Database functions 
func ReadStatus() (res [][]string) { Return slice of slice of strings 
err := db.Ping() 
if err != nil { 
log.Fatal(err) 
} 
rows, err := db.Query("SELECT id, tweet, username FROM posts WHERE 
username = ? ORDER BY id DESC", currentuser) 
if err != nil { 
log.Fatal(err) 
} 
defer rows.Close() 
var tweet, id, username string 
for rows.Next() { 
err := rows.Scan(&id, &tweet, &username) 
if err != nil { 
return res 
} 
var a []string 
a = append(a, id, tweet, username) 
res = append(res, a) 
} 
return 
} 
Verify db connection 
Raw queries 
Append strings to slice 
Append slice of strings to output
Handler functions 
func homeHandler(w http.ResponseWriter, r *http.Request) { 
Create slice with Post instances 
Append slice of Post 
instances to Posts slice 
currentuser = getUserName(r) 
if currentuser != "" { 
var as []Post 
a := User{} 
m := ReadStatus() 
for i := 0; i < len(m); i++ { 
as = append(as, Post{Tweetid: m[i][0], Username: 
currentuser, Status: m[i][1]}) 
} 
a = User{Username: currentuser} 
for i := 0; i < len(m); i++ { 
a.Posts = append(a.Posts, &as[i]) 
} 
renderTemplate(w, "home", "homepage", a) 
} else { 
http.Redirect(w, r, "/", 302) 
} 
} 
Wrapper func around 
html/template library
Func main 
Open connection pool 
Handlers for URL paths 
func main() { 
db, err = sql.Open("mysql", 
os.Getenv("DB_USERNAME")+":"+os.Getenv("DB_PASSWORD")+"@tcp("+os.Getenv("DB 
_CLEARDB")+":3306)/"+os.Getenv("DB_NAME")) 
if err != nil { 
log.Fatal(err) 
} 
defer db.Close() 
router.HandleFunc("/", logHandler) 
router.HandleFunc("/login", loginHandler) 
router.HandleFunc("/home", homeHandler) 
router.HandleFunc("/home/tweets", usertweetHandler).Methods("POST") 
router.HandleFunc("/logout", logoutHandler).Methods("POST") 
router.HandleFunc("/home/delete", deleteHandler).Methods("POST") 
router.HandleFunc("/home/edit", editHandler).Methods("POST") 
router.HandleFunc("/home/save", saveHandler).Methods("POST”) 
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/"))) 
http.Handle("/", router) 
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil) 
if err != nil { 
panic(err) 
} 
} 
Port for server 
connection
2nd app: API Client 
Get a series of photos from Flickr 
for a random city and display 
current weather data for the same 
city
Libraries 
import ( 
"encoding/json" 
"fmt" 
"html/template" 
"io/ioutil" 
"log" 
"math/rand" 
"net/http" 
"os" 
"strconv" 
"time" 
)
Thank you. 
https://github.com/AudreyLim/weathergo 
https://github.com/AudreyLim/twittergo 
audreylh@gmail.com 
Twitter: @AudreyLim77

More Related Content

KEY
MongoDB
PDF
Leveraging the Power of Graph Databases in PHP
PDF
Web Scrapping with Python
PDF
Leveraging the Power of Graph Databases in PHP
ODP
Php 102: Out with the Bad, In with the Good
PDF
My First Ruby
PDF
お題でGroovyプログラミング: Part A
PPTX
Next Generation Language Go
MongoDB
Leveraging the Power of Graph Databases in PHP
Web Scrapping with Python
Leveraging the Power of Graph Databases in PHP
Php 102: Out with the Bad, In with the Good
My First Ruby
お題でGroovyプログラミング: Part A
Next Generation Language Go

What's hot (19)

PDF
Velocity tips and tricks
PPT
Sphinx как база данных
PDF
No sql databases blrdroid devfest 2016
PDF
NoSQL を Ruby で実践するための n 個の方法
PPTX
Grails queries
PDF
from Ruby to Objective-C
PDF
Class 7b: Files & File I/O
PPTX
Android Lab Test : Reading the foot file list (english)
PDF
Neo4j Graph Database และการประยุกตร์ใช้
PPTX
ElasticSearch 5.x - New Tricks - 2017-02-08 - Elasticsearch Meetup
PDF
Ramda, a functional JavaScript library
PDF
Fun with Python
PDF
Beautiful soup
PPTX
.Net (F # ) Records, lists
PDF
Opa presentation at GamesJs
PDF
Embedding a language into string interpolator
KEY
Learning How To Use Jquery #5
PDF
Ast transformations
ODP
One Database To Rule 'em All
Velocity tips and tricks
Sphinx как база данных
No sql databases blrdroid devfest 2016
NoSQL を Ruby で実践するための n 個の方法
Grails queries
from Ruby to Objective-C
Class 7b: Files & File I/O
Android Lab Test : Reading the foot file list (english)
Neo4j Graph Database และการประยุกตร์ใช้
ElasticSearch 5.x - New Tricks - 2017-02-08 - Elasticsearch Meetup
Ramda, a functional JavaScript library
Fun with Python
Beautiful soup
.Net (F # ) Records, lists
Opa presentation at GamesJs
Embedding a language into string interpolator
Learning How To Use Jquery #5
Ast transformations
One Database To Rule 'em All
Ad

Similar to Golang slidesaudrey (20)

PDF
Server Side Swift with Swag
PPTX
Android and REST
PPTX
Net/http and the http.handler interface
PPTX
Net/http and the http.handler interface
PPTX
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
PDF
Symfony + GraphQL
PPTX
Graphql + Symfony | Александр Демченко | CODEiD
ODP
Django tech-talk
PPTX
Building a friendly .NET SDK to connect to Space
PDF
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
PDF
Xitrum @ Scala Matsuri Tokyo 2014
PDF
Coding Ajax
PDF
Writing RESTful web services using Node.js
KEY
CouchDB on Android
PDF
Coding Ajax
PDF
From polling to real time: Scala, Akka, and Websockets from scratch
PDF
Developing web applications in Rust
PPT
Building Single-Page Web Appplications in dart - Devoxx France 2013
ODP
Web program-peformance-optimization
Server Side Swift with Swag
Android and REST
Net/http and the http.handler interface
Net/http and the http.handler interface
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Symfony + GraphQL
Graphql + Symfony | Александр Демченко | CODEiD
Django tech-talk
Building a friendly .NET SDK to connect to Space
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum @ Scala Matsuri Tokyo 2014
Coding Ajax
Writing RESTful web services using Node.js
CouchDB on Android
Coding Ajax
From polling to real time: Scala, Akka, and Websockets from scratch
Developing web applications in Rust
Building Single-Page Web Appplications in dart - Devoxx France 2013
Web program-peformance-optimization
Ad

Golang slidesaudrey

  • 1. How I built my first web app in Go 16 Sep 2014 Audrey Lim [email protected] Twitter: @AudreyLim77
  • 2. About Me Building Go applications
  • 3. About Me Lawyer (1-2 yrs)
  • 4. About Me Lawyer (1-2 yrs) Learning Journey May – Jul 2014: HTML, CSS, JS
  • 5. About Me Lawyer (1-2 yrs) Learning Journey May – Jul 2014: HTML, CSS, JS (codeacademy, books, websites)
  • 6. About Me Lawyer (1-2 yrs) Learning Journey (codeacademy, books, websites) May – Jul 2014: HTML, CSS, JS mid-Jul 2014: Go (1st backend)
  • 7. About Me Lawyer (1-2 yrs) Learning Journey (codeacademy, books, websites) (everywhere) May – Jul 2014: HTML, CSS, JS mid-Jul 2014: Go (1st backend)
  • 8. About Me Lawyer (1-2 yrs) Learning Journey (codeacademy, books, websites) (everywhere) May – Jul 2014: HTML, CSS, JS mid-Jul 2014: Go (1st backend) mid-Aug 2014: deployed 1st app – CRUD app
  • 9. About Me Lawyer (1-2 yrs) Learning Journey (codeacademy, books, websites) (everywhere) May – Jul 2014: HTML, CSS, JS mid-Jul 2014: Go (1st backend) mid-Aug 2014: deployed 1st app – CRUD app end-Aug 2014: deployed 2nd app – API client
  • 10. Go Resources golang.org Books and websites Sample applications Stackoverflow, Go-nuts mailing list
  • 11. Go Resources golang.org (Go Tour, Docs, Go blog) Books and websites Sample applications Stackoverflow, Go-nuts mailing list
  • 12. Go Resources golang.org (Go Tour, Docs, Go blog) Books and websites (www.golang-book.com, www.gobyexample.com) Sample applications Stackoverflow, Go-nuts mailing list
  • 13. Go Resources golang.org (Go Tour, Docs, Go blog) Books and websites (www.golang-book.com, www.gobyexample.com) Sample applications (Go Wiki, from books) Stackoverflow, Go-nuts mailing list
  • 16. CRUD App Simple CRUD App using MySQL for data persistence API Client Flickr, Weather API
  • 17. 1st app: CRUD App Sign up/Sign in Read Status Create Status Delete Status Edit Status Sign out MySQL
  • 18. Basic Structure package main Import ( “fmt” “net/http” ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, “hello world!”) } func main() { http.HandleFunc(“/”, handler) http.ListenAndServe(“:8080”, nil) }
  • 19. Basic Structure package main Import ( “fmt” “net/http” ) All executable commands use package main Print output to client func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, “hello world!”) } func main() { Handler function when root is requested http.HandleFunc(“/”, handler) http.ListenAndServe(“:8080”, nil) } Local server connection Go’s stdlib Entry point
  • 20. Libraries import ( "database/sql" "html/template" "fmt" "log" "net/http" "os" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux" "github.com/gorilla/securecookie” )
  • 21. Libraries Standard library import ( "database/sql" "html/template" "fmt" "log" "net/http" "os" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux" "github.com/gorilla/securecookie” ) External packages
  • 22. Data Structs Type Slice of Pointers to Post struct type User struct { Userid int64 Username string Password string Posts []*Post } type Post struct { Tweetid string Username string Status string }
  • 23. Database functions func ReadStatus() (res [][]string) { Return slice of slice of strings err := db.Ping() if err != nil { log.Fatal(err) } rows, err := db.Query("SELECT id, tweet, username FROM posts WHERE username = ? ORDER BY id DESC", currentuser) if err != nil { log.Fatal(err) } defer rows.Close() var tweet, id, username string for rows.Next() { err := rows.Scan(&id, &tweet, &username) if err != nil { return res } var a []string a = append(a, id, tweet, username) res = append(res, a) } return }
  • 24. Database functions func ReadStatus() (res [][]string) { Return slice of slice of strings err := db.Ping() if err != nil { log.Fatal(err) } rows, err := db.Query("SELECT id, tweet, username FROM posts WHERE username = ? ORDER BY id DESC", currentuser) if err != nil { log.Fatal(err) } defer rows.Close() var tweet, id, username string for rows.Next() { err := rows.Scan(&id, &tweet, &username) if err != nil { return res } var a []string a = append(a, id, tweet, username) res = append(res, a) } return } Verify db connection Raw queries Append strings to slice Append slice of strings to output
  • 25. Handler functions func homeHandler(w http.ResponseWriter, r *http.Request) { Create slice with Post instances Append slice of Post instances to Posts slice currentuser = getUserName(r) if currentuser != "" { var as []Post a := User{} m := ReadStatus() for i := 0; i < len(m); i++ { as = append(as, Post{Tweetid: m[i][0], Username: currentuser, Status: m[i][1]}) } a = User{Username: currentuser} for i := 0; i < len(m); i++ { a.Posts = append(a.Posts, &as[i]) } renderTemplate(w, "home", "homepage", a) } else { http.Redirect(w, r, "/", 302) } } Wrapper func around html/template library
  • 26. Func main Open connection pool Handlers for URL paths func main() { db, err = sql.Open("mysql", os.Getenv("DB_USERNAME")+":"+os.Getenv("DB_PASSWORD")+"@tcp("+os.Getenv("DB _CLEARDB")+":3306)/"+os.Getenv("DB_NAME")) if err != nil { log.Fatal(err) } defer db.Close() router.HandleFunc("/", logHandler) router.HandleFunc("/login", loginHandler) router.HandleFunc("/home", homeHandler) router.HandleFunc("/home/tweets", usertweetHandler).Methods("POST") router.HandleFunc("/logout", logoutHandler).Methods("POST") router.HandleFunc("/home/delete", deleteHandler).Methods("POST") router.HandleFunc("/home/edit", editHandler).Methods("POST") router.HandleFunc("/home/save", saveHandler).Methods("POST”) router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/"))) http.Handle("/", router) err := http.ListenAndServe(":"+os.Getenv("PORT"), nil) if err != nil { panic(err) } } Port for server connection
  • 27. 2nd app: API Client Get a series of photos from Flickr for a random city and display current weather data for the same city
  • 28. Libraries import ( "encoding/json" "fmt" "html/template" "io/ioutil" "log" "math/rand" "net/http" "os" "strconv" "time" )
  • 29. Thank you. https://github.com/AudreyLim/weathergo https://github.com/AudreyLim/twittergo [email protected] Twitter: @AudreyLim77