Skip to content

Kong ​

Before You Start

  • Applies to: Go 1.21+ (examples verified with Go 1.22) · Prerequisites: Std: http · Read time: about 8 min

Documentation: (konghq.com)

Open Source Repository: Kong/kong: 🦍 The Cloud-Native API Gateway (github.com)

Complete Example ​

The program below runs as-is: copy it into a file and execute go run main.go, or paste it into the Go Playground.

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	path := "/api/v1/users"

	fmt.Println("matches /api prefix:", strings.HasPrefix(path, "/api/"))
	stripped := strings.TrimPrefix(path, "/api")
	fmt.Println("stripped path:", stripped)
	fmt.Println("upstream URL:", "http://user-service:8080"+stripped)
}

Expected output:

text
matches /api prefix: true
stripped path: /v1/users
upstream URL: http://user-service:8080/v1/users

Common Pitfalls ​

  • Route priority is subtle. Exact matches beat prefixes, and regex routes are evaluated with their own rules — test matchers explicitly.
  • Plugin order matters. Authentication and rate limiting must run before business plugins; each plugin has a priority value.
  • Health checks belong on the upstream. Configure active/passive checks so unhealthy targets stop receiving traffic.
  • Keep the gateway thin. Kong routes and protects; it is not the place for business logic.
  • Protect the admin API. Bind it to an internal interface and require authentication.

Change Log ​

  • 2026-10-14: Added "Before You Start" notes, a complete runnable example with expected output, common pitfalls, and this change log.

Golang by www.golangdev.cn · Adapted in part from Golang 中文学习文档 (MIT, © 寒江蓑笠翁)