Skip to content

Instantly share code, notes, and snippets.

@s1s1ty
s1s1ty / post.sql
Created July 10, 2018 14:45
table create
CREATE DATABASE IF NOT EXISTS `go-mysql-crud` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */;
USE `go-mysql-crud`;
/* Create table for `posts` */
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`content` longtext COLLATE utf8_unicode_ci NOT NULL,
@s1s1ty
s1s1ty / ps.txt
Created July 18, 2018 10:48
os process show/kill
pid aux
kill -9 <pid>
@s1s1ty
s1s1ty / go-sql-2.go
Created July 18, 2018 17:01
2nd part
func init() {
router = chi.NewRouter()
router.Use(middleware.Recoverer)
dbSource := fmt.Sprintf("root:%s@tcp(%s:%s)/%s?charset=utf8", dbPass, dbHost, dbPort, dbName)
var err error
db, err = sql.Open("mysql", dbSource)
catch(err)
@s1s1ty
s1s1ty / go-sql-4.go
Created July 18, 2018 17:07
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)
@s1s1ty
s1s1ty / go-sql-5.go
Created July 18, 2018 17:08
5th part
// UpdatePost update a spesific post
func UpdatePost(w http.ResponseWriter, r *http.Request) {
var post Post
id := chi.URLParam(r, "id")
json.NewDecoder(r.Body).Decode(&post)
query, err := db.Prepare("Update posts set title=?, content=? where id=?")
catch(err)
_, er := query.Exec(post.Title, post.Content, id)
catch(er)
@s1s1ty
s1s1ty / go-sql-6.go
Created July 18, 2018 17:09
6th part
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func catch(err error) {
func main() {
routers()
http.ListenAndServe(":8005", Logger())
}
@s1s1ty
s1s1ty / main.go
Created July 31, 2018 07:15
go-sql-main
package main
import (
"fmt"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/s1s1ty/go-mysql-crud/driver"
@s1s1ty
s1s1ty / driver.go
Created July 31, 2018 07:30
driver-go-mysql-crud
package driver
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
// DB ...
@s1s1ty
s1s1ty / handler.go
Last active August 3, 2018 12:04
handler go mysql crud
package handler
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/s1s1ty/go-mysql-crud/driver"