Treasureに向けて環境をセットアップしておきましょう。
Windowsローカルでの環境構築、筆者は詳しくありません。どうしてもWindowsローカルでそのまま開発したい方以外はVMにLinuxの環境をつくっておくとよいでしょう。各資料では基本的にbashないしzshなどのシェルを利用した例を載せていますので、そのほうがわかりやすいと思います。ディストリビューションは好きなものをどうぞ。
../go/README.md をみてください。
講義ではMySQL 5.6を利用します。
Dockerを使えば検証用のMySQL環境を簡単に整えることができます。
Mac OS XならDocker for Macをつかうとよいでしょう。
https://docs.docker.com/engine/installation/mac/
WindowsだとDocker for Windowsをつかうといいです。
https://docs.docker.com/engine/installation/windows/
Linuxは各ディストリビューション向けのDockerをいれてください。
# Dockerコンテナの立ち上げ
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6
# Dockerプロセスの確認
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69abd53a80f9 mysql:5.6 "docker-entrypoint.sh" 6 seconds ago Up 4 seconds 3306/tcp some-mysql
# Dockerコンテナ内のMySQLクライアントをつかってコンテナ内のMySQLプロセスに接続
$ docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
詳しくはDocker Hub _ mysqlの説明をみてください。
ローカルにMySQLをそのままいれてもいいです。
OSXだとHomeBrewをつかっていれるのが楽でしょう。
$ brew install mysql56
Linuxは各ディストリビューションのインストール指示にしたがってください。
Windowsでもサービスとしてインストールすることはできますが、それよりはVM上のLinuxにいれるほうがおすすめです。
開発用だけならSQLiteをつかうのもいいアイデアです。筆者は普段さくっとDBをつかったもののプロトタイプを作るにはSQLiteをつかっています。
SQLiteは組み込み用途にも使われる小さいDBです。例えばブラウザやスマホアプリに組み込まれて利用することができるようになっていたりします。MySQLももちろんファイルベースではありますが、SQLiteは1ファイルにおさまり、かつ、小さいDB実装なのでわかりやすいはずです。
例えばこんな感じでつかえます。
-> % sqlite3 hoge.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> create table blog(id integer primary key, body text, title text);
sqlite> insert into blog values(1, 'hoge', 'this is title');
sqlite> select * from blog;
1|hoge|this is title
sqlite3のDBファイルは単一のファイルになっています。
$ ls -la hoge.db
-rw-r--r-- 1 ke-suzuki wheel 8.0K 8 2 18:11 hoge.db
$ file hoge.db
hoge.db: SQLite 3.x database
ただし単一のSQLiteファイルを複数プロセスからopenすることはできなかったりするので、そのあたりは工夫しないといけません。