Menu

Expand
Rate 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 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

Install Go

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:

$ go version
go version go1.19 darwin/amd64

Install a text editor or IDE

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.

Start a new Go project

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.

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

Now you're ready to install your Go dependencies.

Install Gin and the Twilio Go SDK

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

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.

package main

import (
  "net/http"

  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // listen and serve on localhost:8080
}

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.

Install ngrok for local development

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:

ngrok by @inconshreveable                                                           (Ctrl+C to quit)

Session Status   online
Account          <Your name> (Plan: Free)
Version          2.3.40
Region           United States (us)
Web Interface    http://127.0.0.1:4040
Forwarding       http://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:3000
Forwarding       https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:3000

Connections      ttl     opn     rt1     rt5     p50     p90
                 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".

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?

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

Shawn Stern David Prothero Kevin Whinnery Kat King Samuel Mendes Andrew Baker Jose Oliveros Paul Kamp
Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more