In this guide, we'll cover how to set up your Go development environment for a Gin project. We'll also walk through some helpful tools that we recommend for all Go applications that use Twilio: ngrok and the Twilio Go SDK
How you install Go varies depending on your operating system, but there's an installer for every OS. Follow the installation instructions on the Go install page, then come back to this tutorial.
Once Go is installed, you should be able to verify your installation by entering go version
into your terminal. You will see a response, similar to this, that corresponds to your version of Go and your OS/system architecture:
1$ go version2go version go1.19 darwin/amd64
Before we can start a Go project, you'll need a place to write your code.
If you already have a code-writing tool of choice, you can stick with it for developing your Go application. If you're looking for something new, we recommend using Visual Studio Code and installing the official Go extension. It's recommended by the Go team, and many developers here at Twilio and in the wider Go ecosystem are extremely happy using it.
Before starting any new Go project, you should run go mod init
to create a new go.mod
file for your project.
Create a new empty directory in your development environment, enter it, and run go mod init <your-project-name>
. go
will create a new go.mod
file for you, which can prove invaluable for installing dependencies for running an HTTP server, using Twilio, and more.
1$ go mod init go-example2go: creating new go.mod: module go-example
Now you're ready to install your Go dependencies.
We're almost ready to write a Gin web application, but first, we need to install the Gin module. Use the below Go command:
go get -u github.com/gin-gonic/gin
These commands tell go
to add the gin
module to your project's go.mod
file. This makes the module available for use, and, if you ever want to install your module(s) again in the future - like on a production server - you can just run go mod download
.
You can test that you configured your development environment correctly by creating a minimally-featured Gin application. Grab this short example from the Gin quickstart, drop it in a new file named example.go, and save the file.
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}
You can then run your application with the command go run example.go
. If you open http://localhost:8080/ping in your browser, you should see the "pong" response.
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 our favorite 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
. 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:30009Forwarding https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:30001011Connections 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. Here, ours is "https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io".
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 now ready to build out your Express application! Here are a few other sample applications we've built: