# 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
- Clone the starter Go application:
git clone https://github.com/planetscale/golang-example
- Navigate into the folder:
cd golang-example
- Copy the
.env.example
file into.env
:
cp .env.example .env
Set up the database
- Sign up for a free PlanetScale account.
- Create a new database. A default branch,
main
, will be created for you.
Connect to the Go app
- On the database overview page in the PlanetScale dashboard, click "Connect".
- Click "New password".
- In the "Connect to" dropdown, select Go.
- Copy the connection string.
- Open your
.env
file and paste the connection string in as the value forDSN
. You're now connected!
Run migrations and seeder
- Start the Go app:
go run .
-
Navigate to
localhost:8080/seed
to run the migrations and the seeder. -
View the product and category data as follows:
- Get all products —
localhost:8080/products
- Get all categories —
localhost:8080/categories
- Get a single product —
localhost:8080/product/{id}
- Get a single category —
localhost:8080/category/{id}
# Functions
NewHandler creates an http.Handler which wraps a PlanetScale database connection.