Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

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


Prerequisites

prerequisites page anchor

Before you begin this tutorial, do the following:


Set up your Go and Gin development environment

set-up-your-go-and-gin-development-environment page anchor

Using VS Code, create a directory and configure your Gin settings for your app:

  1. Open VS Code.

  2. To open the terminal in VS Code, go to View > Terminal.

  3. Create a directory for your project.

    1
    mkdir my-twilio-project
    2
    cd my-twilio-project
    3
    code .
  4. Before starting any Go project, run go mod init to create a go.mod file for your project:

    go mod init go-example

    This command creates a go.mod file for you, which is helpful for installing dependencies(link takes you to an external page) for running an HTTP server, using Twilio, and more.

  5. Install the required dependencies for the Gin web framework:

    go get -u github.com/gin-gonic/gin

Create a sample Gin application

create-a-sample-gin-application page anchor

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:

  1. Add a file called main.go to your workspace.

  2. Add the following code:

    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
    }
  3. Save this file in your workspace.

  4. Run your Gin application in the terminal.

    go run main.go &
  5. 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.


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 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(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.

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:8080
9
Forwarding https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:8080
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. 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: