Golang の echo で MariaDB の CRUD

こちらのプログラムに機能を追加しました。
[Golang の echo で MariaDB の Read](https://qiita.com/ekzemplaro/items/6abb469a86ece37319fd)

```golang:server.go
// -----------------------------------------------------------------------
/*
	server.go

					Jun/12/2018
*/
// -----------------------------------------------------------------------
package main

import (
	"os"
	"fmt"
	"net/http"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"

	_ "github.com/go-sql-driver/mysql"

	"github.com/gocraft/dbr"
)

type (
	cityinfo struct {
	    ID   string     `db:"id"`
	    Name string  `db:"name"`
	    Population int  `db:"population"`
	    Date_mod string  `db:"date_mod"`
	}

	responseData struct {
	Cities        []cityinfo      `json:"cities"`
	}
)

var (
	tablename = "cities"
	seq   = 1
	conn, _ = dbr.Open("mysql", "scott:tiger123@tcp(127.0.0.1:3306)/city", nil)
	sess = conn.NewSession(nil)
)

// -----------------------------------------------------------------------
func insertCity(cc echo.Context) error {
	fmt.Fprintf (os.Stderr,"*** insertCity ***\n")
	uu := new(cityinfo)
	if err := cc.Bind(uu); err != nil {
	    return err
	}

	fmt.Fprintf (os.Stderr,"*** insertCity *** ccc ***\n")

	sess.InsertInto(tablename).Columns("id","name","population","date_mod").Values(uu.ID,uu.Name,uu.Population,uu.Date_mod).Exec()


	return cc.NoContent(http.StatusOK)
}


// -----------------------------------------------------------------------
func selectCities(cc echo.Context) error {
	fmt.Fprintf (os.Stderr,"*** selectCities ***\n")
	var uu []cityinfo

	sess.Select("*").From(tablename).Load(&uu)
	response := new(responseData)
	response.Cities = uu
	return cc.JSON(http.StatusOK,response)
}

// -----------------------------------------------------------------------
func selectCity(cc echo.Context) error {
	fmt.Fprintf (os.Stderr,"*** selectCity ***\n")
	var mm cityinfo
	id := cc.Param("id")
	sess.Select("*").From(tablename).Where("id = ?", id).Load(&mm)
	return cc.JSON(http.StatusOK,mm)
}

// -----------------------------------------------------------------------
func updateCity(cc echo.Context) error {
	fmt.Fprintf (os.Stderr,"*** updateCity ***\n")

	uu := new(cityinfo)
	if err := cc.Bind(uu); err != nil {
	    return err
	}

	fmt.Fprintf (os.Stderr,"*** updateCity *** ccc ***\n")

	attrsMap := map[string]interface{}{"id": uu.ID, "name": uu.Name , "population" : uu.Population , "date_mod" : uu.Date_mod}
	sess.Update(tablename).SetMap(attrsMap).Where("id = ?", uu.ID).Exec()
	return cc.NoContent(http.StatusOK)
}

// -----------------------------------------------------------------------
func deleteCity(cc echo.Context) error {
	fmt.Fprintf (os.Stderr,"*** deleteCity ***\n")
	id := cc.Param("id")

	sess.DeleteFrom(tablename).
	Where("id = ?", id).
	Exec()

	return cc.NoContent(http.StatusNoContent)
}


// -----------------------------------------------------------------------
func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	// Routes

	e.POST("/cities/", insertCity)
	e.GET("/city/:id", selectCity)
	e.GET("/cities/",selectCities)
	e.PUT("/cities/", updateCity)
	e.DELETE("/cities/:id", deleteCity)

	// Start server
	e.Start(":1323")
}


// -----------------------------------------------------------------------
```

サーバーの起動

```bash
go run server.go
```

データの一覧を JSON で取得

```bash
curl http://localhost:1323/cities/ | jq .
```

あるID　のデータを JSON で取得

```bash
curl http://localhost:1323/city/t3328 | jq .
```

データのインサート

```bash
curl -X POST -H 'Content-Type: application/json' \
	-d '{"ID" : "t3330" , "Name" : "下野" , "Population" : 123400 , "Date_mod" : "2018-6-12" }' \
	localhost:1323/cities/
```

データの更新

```bash
curl -X PUT -H 'Content-Type: application/json' \
	-d '{"id" : "t3328" , "name" : "高梁" , "population" : 154000 , "date_mod" : "2018-6-12" }' localhost:1323/cities/
```

データの削除

```bash
curl -X DELETE localhost:1323/cities/t3327
```