Categorygithub.com/planetscale/golang-example
modulepackage
0.0.0-20230113210558-681f5931a7ef
Repository: https://github.com/planetscale/golang-example.git
Documentation: pkg.go.dev

# README

Learn how to integrate PlanetScale with a sample Go application

This sample application demonstrates how to connect to a PlanetScale MySQL database, create and run migrations, seed the database, and display the data.

For the full tutorial, see the Go PlanetScale documentation.

Set up the Go app

  1. Clone the starter Go application:
git clone https://github.com/planetscale/golang-example
  1. Navigate into the folder:
cd golang-example
  1. Copy the .env.example file into .env:
cp .env.example .env

Set up the database

  1. Sign up for a free PlanetScale account.
  2. Create a new database. A default branch, main, will be created for you.

Connect to the Go app

  1. On the database overview page in the PlanetScale dashboard, click "Connect".
  2. Click "New password".
  3. In the "Connect to" dropdown, select Go.
  4. Copy the connection string.
  5. Open your .env file and paste the connection string in as the value for DSN. You're now connected!

Run migrations and seeder

  1. Start the Go app:
go run .
  1. Navigate to localhost:8080/seed to run the migrations and the seeder.

  2. View the product and category data as follows:

# Functions

NewHandler creates an http.Handler which wraps a PlanetScale database connection.

# Structs

A Category describes a group of Products.
A Handler is an HTTP API server handler.
A Product contains metadata about a product for sale.