Building Web Applications With Go (Part 3)

in #steemstem8 years ago (edited)

What Will You Learn?

  • You will learn how to add an SQL database into a Go Web application
  • You will learn how to properly error check the core Go SQL ORM
  • You will learn how to open and execute various SQL queries in Go

Requirements

  • Basic knowledge of SQL databases
  • Code from Parts 1 and 2
  • Go 1.6+ installed on your machine with the GOPATH and GOROOT setup properly
  • A text editor that supports Go

Difficulty

  • Basic

Description

In this tutorial, we look at how we can add an SQLite3 database to our Go Web application. Up until now, we have stored the data that is served onto our web pages by using text documents. This is a fairly inefficient way to store data because it isn't concurrent and the file readers are blocking. This means that only one instance of the server can access a document at a given time. As our website grows in size and we add more data to the existing server, this will become a problem. It will prevent our Web application from scaling properly and as a result, make it inefficient.

This is why we add the SQL layer into our application. The SQL database not only allows us to store more data in a much more efficient way; it lets more server instances access this data at a given time. While SQLite3 is just a embedded database; it serves our purposes fairly well for this particular application. Also, because of the way that Go interfaces with ORM SQL services, you could easily expand this application out to use PostgreSQL, MySQL or any other SQL data store without changing any of the core logic. You could also add some non-SQL databases to this application such as Redis, MongoDB and Datomic with minimal effort. In this particular example, we also keep our data storage as a backup to allow us to load the source data into our databases in a fairly easy way so that we can expand on our current data set without running into problems.

The source code for this project can be found here

Note: this is not an SQL tutorial, some of the SQL queries could certainly be formatted for better optimization and security.

Video Tutorial

Curriculum

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.083
BTC 60866.94
ETH 1568.75
USDT 1.00
SBD 0.50