Skip to contentSkip to navigationSkip to topbar
On this page

Set up your Go and Gin development environment


In this guide, we'll cover how to set up your Go development environment for a Gin(link takes you to an external page) 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(link takes you to an external page)


Install Go

install-go page anchor

How you install Go varies depending on your operating system, but there's an installer for every OS. Follow the installation instructions(link takes you to an external page) 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 version
2
go version go1.19 darwin/amd64

Install a text editor or IDE

install-a-text-editor-or-ide page anchor

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(link takes you to an external page) and installing the official Go extension(link takes you to an external page). It's recommended by the Go team(link takes you to an external page), 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(link takes you to an external page) for running an HTTP server, using Twilio, and more.

1
$ go mod init go-example
2
go: creating new go.mod: module go-example

Now you're ready to install your Go dependencies.


Install Gin and the Twilio Go SDK

install-gin-and-the-twilio-go-sdk page anchor

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.


Create a Gin application

create-a-gin-application page anchor

You can test that you configured your development environment correctly by creating a minimally-featured Gin application. Grab this short example(link takes you to an external page) from the Gin quickstart, drop it in a new file named example.go, and save the file.

1
package main
2
3
import (
4
"net/http"
5
6
"github.com/gin-gonic/gin"
7
)
8
9
func main() {
10
r := gin.Default()
11
r.GET("/ping", func(c *gin.Context) {
12
c.JSON(http.StatusOK, gin.H{
13
"message": "pong",
14
})
15
})
16
r.Run() // listen and serve on localhost:8080
17
}

You can then run your application with the command go run example.go. If you open http://localhost:8080/ping(link takes you to an external page) in your browser, you should see the "pong" response.


Install ngrok for local development

install-ngrok-for-local-development page anchor

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(link takes you to an external page).

Most Twilio services use webhooks(link takes you to an external page) 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(link takes you to an external page)

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:

1
ngrok by @inconshreveable (Ctrl+C to quit)
2
3
Session Status online
4
Account <Your name> (Plan: Free)
5
Version 2.3.40
6
Region United States (us)
7
Web Interface http://127.0.0.1:4040
8
Forwarding http://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:3000
9
Forwarding https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:3000
10
11
Connections ttl opn rt1 rt5 p50 p90
12
0 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(link takes you to an external page)".

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.


Where to next with Go and Gin?

where-to-next-with-go-and-gin page anchor

You're now ready to build out your Express application! Here are a few other sample applications we've built:

More Go and Gin resources and guides

more-go-and-gin-resources-and-guides page anchor

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.