LoginSignup
2
1

More than 5 years have passed since last update.

Golang の echo で MariaDB の Read

Posted at

次のページを参考にしました。
Golang+Echo+dbrでMySQLのCRUDをする/JSONでDBの値を返却する話

MariaDB から読んだデータを、JSONで出力します。

Mysql への接続情報

User: scott
Password: tiger123
データベース: city
テーブル名: cities

MariaDB の状態

$ mysql -uscott -ptiger123 city
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.1.33-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [city]> select * from cities;
+-------+--------+------------+------------+
| id    | name   | population | date_mod   |
+-------+--------+------------+------------+
| t3321 | 岡山   |     725139 | 2002-2-9   |
| t3322 | 倉敷   |     417328 | 2002-5-15  |
| t3323 | 津山   |     891654 | 2002-7-21  |
| t3324 | 玉野   |     265981 | 2002-11-12 |
| t3325 | 笠岡   |     284597 | 2002-4-24  |
| t3326 | 井原   |     671942 | 2002-2-8   |
| t3327 | 総社   |     265481 | 2002-8-3   |
| t3328 | 高梁   |     792356 | 2002-3-14  |
| t3329 | 新見   |     415892 | 2002-1-15  |
+-------+--------+------------+------------+
9 rows in set (0.00 sec)

MariaDB [city]>
server.go
// -----------------------------------------------------------------------
/*
    server.go

                        Jun/11/2018
*/
// -----------------------------------------------------------------------
package main

import (
    "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 {
    Users   []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 selectCities(c echo.Context) error {
    var u []cityinfo

    sess.Select("*").From(tablename).Load(&u)
    response := new(responseData)
    response.Users = u
    return c.JSON(http.StatusOK,response)
}

// -----------------------------------------------------------------------
func main() {
    e := echo.New()

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

    // Routes

    e.GET("/cities/",selectCities)

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

// -----------------------------------------------------------------------

サーバーの起動

go run server.go

クライアントでアクセス

curl http://localhost:1323/cities/ | jq .
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1