goによるwebアプリ開発のキホン

33

Upload: akihiko-horiuchi

Post on 07-Jan-2017

19.159 views

Category:

Technology


2 download

TRANSCRIPT

○ goroutine channel

○ go func() go

○ gofmt goimports

○ *_test.go

○ go test

func TestSum(t *testing.T) { actual := Sum(10, 20) expected := 30 if actual != expected { t.Fatal("want", expected, "got", actual) }}

○ rake db:migrate

○ "text/template" "html/template"

"net/http"

○ http.HandleFunc

"/users/:id"

○ http.ListenAndServe

○ "log"

func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, World!") log.Printf("%+v¥n", r) } ) http.ListenAndServe(":8080", nil)}

○ "net/http"

github.com/zenazn/goji

github.com/gin-gonic/gin

github.com/gorilla/mux

github.com/revel/revel

● github.com/zenazn/goji

● github.com/gorilla/mux

router := goji.New()router.Get("/:name", handler)

func handler(c goji.C, w http.ResponseWriter, r ...) { name := c.URLParams["name"] fmt.Fprintf(w, "Hello, %s!¥n", name)}

router := gorilla.NewRouter()router.HandleFunc("/{name}", handler)

func handler(w http.ResponseWriter, r *http.Request) { name := gorilla.Vars(r)["name"] fmt.Fprintf(w, "Hello, %s!¥n", name)}

● bitbucket.org/liamstask/goose

○ create up down

$ goose create create_postsgoose: created db/migrations/20160226202023_create_posts.sql

$ goose upgoose: migrating db environment 'development', ...OK 20160226202023_create_posts.sql

$ goose downgoose: migrating db environment 'development', ...OK 20160226202023_create_posts.sql

○ sql.Tx.Exec

○ AUTO_INCREMENT PRIMARY KEY

-- +goose Up-- SQL in section 'Up' is executed when this migration is ...CREATE TABLE IF NOT EXISTS `posts` ( `id` INT NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `body` TEXT NOT NULL, PRIMARY KEY (`id`));

-- +goose Down-- SQL section 'Down' is executed when this migration is ...DROP TABLE `posts`;

type Model struct { ID uint `gorm:"primary_key"` CreatedAt time.Time UpdatedAt time.Time DeletedAt *time.Time}

type User struct { gorm.Model Name string}

● github.com/jinzhu/gorm

● github.com/naoina/genmai

db.CreateTable(&User{})user := User{Name: "Kagawa"}db.Create(user)db.Where(&User{Name: "Kagawa"}).First(&user)

db.CreateTable(&User{})user := User{Name: "Kagawa"}db.Insert(&user)db.Select(&users, db.Where("name", "=", "Kagawa"))

"html/template"

○ {{}}

○ template.ParseFiles

type Page struct { Name string}

func handler(w http.ResponseWriter, r *http.Request) { tmpl, _ := template.New("tmpl").Parse("Hello, {{.Name}}!") tmpl.Execute(w, Page{Name: "Golang"})}

func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil)}

● github.com/yosssi/ace

○ ace.Load *template.Template

○ render partial

= doctype htmlhtml lang=en head title Hello Ace = css .blue { color: blue; } body h1.blue {{.Msg}} p.. Ace is an HTML template engine for Go. = javascript console.log('Welcome to Ace');

● github.com/tools/godep

○ Godeps.json Godeps/_workspace

● github.com/mattn/gom

○ gom install gom exec gom build

{ "Deps": [{ "ImportPath": "code.google.com/p/go-netrc/netrc", "Rev": "28676070ab99" }]}

○ gom install

gom 'github.com/yosssi/ace'gom 'github.com/zenazn/goji'

group :development do gom 'github.com/pilu/fresh' gom 'golang.org/x/tools/cmd/goimports'end

$ gom installdownloading github.com/google/go-github/githubdownloading github.com/yosssi/ace...

● github.com/pilu/fresh

$ gom exec fresh -c runner.confLoading settings from runner.conf22:7:30 runner | InitFolders22:7:30 runner | mkdir ./tmp22:7:30 runner | mkdir ./tmp: file exists22:7:30 watcher | Watching .22:7:30 watcher | Watching app22:7:30 watcher | Watching app/controllers22:7:30 watcher | Watching app/views

○ ゙ ゚ ゙ ゙

○ ゙ ゚

router := goji.New()router.Get("/api/users/:name", handler)

func handler(c goji.C, w http.ResponseWriter, r ...) { var user User name := c.URLParams["name"] db.Where(&User{Name: name}).First(&user) bytes, _ := json.Marshal(user) fmt.Println(w, string(bytes)) w.Header.Set("Content-Type", "application/json")}