课程: Practice It: Go REST API Server

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

The database

The database

- [Instructor] Now that we have some basics, let's discuss the database. We need to store the products and order somewhere, so that we can access them through our APIs. We'll use SQLite, a lightweight file based database. If you remember, we installed the module already. I've already created the database with tables and some data and provided it, so we will be able to connect to that in our application. Since it's a simple file based DB, we won't need a username and password. The file is named practiceit.db, and it should be at the top level of the main application folder. In order to access the DB, we need to import database/sql. And then github.com/mattn/go-sqlite3, which is the Go SQLite library. Then since the DB already exists, we just need to connect or open it to get access. From there, it's pretty straightforward. We create a database reference by adding the line, db, comma, error, equals, sql.Open, and…

内容