Skip to content

Instantly share code, notes, and snippets.

@s1s1ty s1s1ty/go-sql-4.go
Created Jul 18, 2018

Embed
What would you like to do?
4th part
// CreatePost create a new post
func CreatePost(w http.ResponseWriter, r *http.Request) {
var post Post
json.NewDecoder(r.Body).Decode(&post)
query, err := db.Prepare("Insert posts SET title=?, content=?")
catch(err)
_, er := query.Exec(post.Title, post.Content)
catch(er)
defer query.Close()
respondwithJSON(w, http.StatusCreated, map[string]string{"message": "successfully created"})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.