SDK
Go
Installation
Setup a project by making a directory:
mkdir hello-ngrok && cd hello-ngrok
Install go on your system here or use Homebrew:
brew install go
Initialize your go project and install the ngrok-go package:
go mod init hello-ngrok && go get golang.ngrok.com/ngrok/v2
Now we need a runnable example. Let’s create a new entry file:
touch main.go
Put your app online
Start a network listener to establish an ngrok tunnel for HTTP requests:
hello-ngrok/main.go
package main
import (
"context"
"fmt"
"log"
"net/http"
"golang.ngrok.com/ngrok/v2"
)
func main() {
l, err := ngrok.Listen(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("endpoint url: ", l.URL())
http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello from your ngrok-delivered Go app!")
}))
}
Run your Go app with your ngrok authtoken as an environment variable. Sign up for a free account to get your authtoken.
NGROK_AUTHTOKEN=<token> go run main.go