Skip to content

Instantly share code, notes, and snippets.

@nclundsten

nclundsten/all.go

Created Jul 27, 2018
Embed
What would you like to do?
i suck at golang
package main
// localhost:8080/create?first=myfirstname
import (
"net/http"
"fmt"
)
// ----person----
type person struct {
id uint64
first string
last string
}
// ----controller----
type controller struct {
mapper mapper
}
func NewController(mapper mapper) controller {
var controller controller
controller.mapper = mapper
return controller
}
func (c controller) create (w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var person person
person.first = fmt.Sprintf("%s", r.Form["first"])
c.mapper.create(person)
}
func (c controller) read (w http.ResponseWriter, r *http.Request) {
}
func (c controller) update (w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var person person
c.mapper.update(person)
}
func (c controller) delete (w http.ResponseWriter, r *http.Request) {
}
func (c controller) list (w http.ResponseWriter, r *http.Request) {
}
// -----main-----
func main() {
mapper := NewMapper("asdf")
controller := NewController(mapper)
// routing
http.HandleFunc("/create", func(w http.ResponseWriter, r *http.Request) {
controller.create(w,r)
})
http.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) {
controller.read(w,r)
})
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
controller.update(w,r)
})
http.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) {
controller.delete(w,r)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
controller.list(w,r)
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
// ----mapper----
type mapper struct {
}
func NewMapper(dsn string) mapper {
var mapper mapper
return mapper
}
func (m mapper) create(person person) {
fmt.Println("create")
fmt.Printf("%+v\n", person)
}
func (m mapper) read(id uint64) {
fmt.Println("read")
}
func (m mapper) update(person person) {
fmt.Printf("%+v\n", person)
fmt.Println("update")
}
func (m mapper) delete(id uint64) {
fmt.Println("delete")
}
func (m mapper) list() {
fmt.Println("list")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment