Download ngrok
ngrok is your app’s front door—and the fastest way to put anything on the internet.
ngrok is your app’s front door—and the fastest way to put anything on the internet.
Setup a project by making a directory:
mkdir hello-ngrok && cd hello-ngrokInstall go on your system here or use Homebrew:
brew install goInitialize your go project and install the ngrok-go package:
go mod init hello-ngrok && go get golang.ngrok.com/ngrok/v2Now we need a runnable example. Let’s create a new entry file:
touch main.goStart a network listener to establish an ngrok tunnel for HTTP requests:
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