Set up your Go and Gin development environment
This tutorial covers how to set up your Go and Gin development environment with a sample application and Twilio webhook configuration.
Time to complete: Approximately 20-45 minutes
Before you begin this tutorial, do the following:
- Create a Twilio account (free tier available).
- Purchase a Twilio phone number.
- Install Go 1.19 or later.
- Intsall ngrok.
- Install Visual Studio Code or another IDE.
Using VS Code, create a directory and configure your Gin settings for your app:
-
Open VS Code.
-
To open the terminal in VS Code, go to View > Terminal.
-
Create a directory for your project.
1mkdir my-twilio-project2cd my-twilio-project3code . -
Before starting any Go project, run
go mod initto create ago.modfile for your project:go mod init go-exampleThis command creates a
go.modfile for you, which is helpful for installing dependencies for running an HTTP server, using Twilio, and more. -
Install the required dependencies for the Gin web framework:
go get -u github.com/gin-gonic/gin
To test that you configured your development environment correctly, you can use the following code to create a minimally-featured Gin application, and verify its response in a browser:
-
Add a file called
main.goto your workspace. -
Add the following code:
1package main23import (4"net/http"56"github.com/gin-gonic/gin"7)89func main() {10r := gin.Default()11r.GET("/ping", func(c *gin.Context) {12c.JSON(http.StatusOK, gin.H{13"message": "pong",14})15})16r.Run() // listen and serve on localhost:808017} -
Save this file in your workspace.
-
Run your Gin application in the terminal.
go run main.go & -
Open your browser to
http://localhost:8080/ping. The browser should display:{"message":"pong"}This verifies that you properly configured your Go and Gin development environment and you can access your app locally.
Once you see your sample Gin application's "pong" response, your development environment is ready to go. However, for most Twilio projects, you'll want to install one more helpful tool: ngrok.
Most Twilio services use webhooks to communicate with your application. When Twilio receives an incoming phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.
When you're working on your Gin application in your development environment, your app is only reachable by other programs on the same computer, so Twilio won't be able to talk to it.
Ngrok is a helpful tool for solving this problem. Once started, it provides a unique URL on the ngrok.io domain, which will forward incoming requests to your local development environment.
To start, head over to the ngrok download page and grab the binary for your operating system: https://ngrok.com/download
Once downloaded, make sure your Gin application is running, and then start ngrok using the command ngrok http 8080.
ngrok http 8080
You should see output similar to this:
1ngrok by @inconshreveable (Ctrl+C to quit)23Session Status online4Account <Your name> (Plan: Free)5Version 2.3.406Region United States (us)7Web Interface http://127.0.0.1:40408Forwarding http://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:80809Forwarding https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:80801011Connections ttl opn rt1 rt5 p50 p90120 0 0.00 0.00 0.00 0.00
Your unique ngrok domain name will be visible on the "Forwarding" line. In this example, it's https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io.
If you open your ngrok forwarding URL with /ping appended (e.g., https://your-ngrok-url.ngrok.io/ping) in your browser, you should see the "pong" response. If everything is working correctly, you should be able to open that domain name in your browser and see your Gin application's "pong" response displayed at your new ngrok URL.
Anytime you're working on your Twilio application and need a URL for a webhook, use ngrok to get a publicly accessible URL like this one.
You're ready to build out your Go application. Learn more with the following resources: