Bump minimum Go and use new features (#1017)

Go 1.21 brings us the builtin min func and slices.Contains.

Go 1.22 brings us range over int and fixed for loop scoping.

We were also able to drop the build tags from the path value code and
inline the three-line function directly. This should still work on
tinygo as they claim to support Go 1.24.
This commit is contained in:
James Raspass 2025-09-17 09:51:46 +01:00 committed by GitHub
parent 9b9fb55def
commit a52c582b53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 24 additions and 79 deletions

View file

@ -20,7 +20,7 @@ jobs:
strategy:
matrix:
go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.x, 1.24.x]
go-version: [1.22.x, 1.23.x, 1.24.x, 1.25.x]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}

2
go.mod
View file

@ -2,4 +2,4 @@ module github.com/go-chi/chi/v5
// Chi supports the four most recent major versions of Go.
// See https://github.com/go-chi/chi/issues/963.
go 1.20
go 1.22

View file

@ -96,7 +96,6 @@ func TestCompressor(t *testing.T) {
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
resp, respString := testRequestWithAcceptedEncodings(t, ts, "GET", tc.path, tc.acceptedEncodings...)
if respString != "textstring" {

View file

@ -2,6 +2,7 @@ package middleware
import (
"net/http"
"slices"
"strings"
)
@ -29,13 +30,7 @@ func contentEncoding(ce string, charsets ...string) bool {
_, ce = split(strings.ToLower(ce), ";")
_, ce = split(ce, "charset=")
ce, _ = split(ce, ";")
for _, c := range charsets {
if ce == c {
return true
}
}
return false
return slices.Contains(charsets, ce)
}
// Split a string in two parts, cleaning any whitespace.

View file

@ -37,7 +37,7 @@ func TestThrottleBacklog(t *testing.T) {
// The throttler processes 10 consecutive requests, each one of those
// requests lasts 1s. The maximum number of requests this can possible serve
// before the clients time out (5s) is 40.
for i := 0; i < 40; i++ {
for i := range 40 {
wg.Add(1)
go func(i int) {
defer wg.Done()
@ -75,7 +75,7 @@ func TestThrottleClientTimeout(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
for i := range 10 {
wg.Add(1)
go func(i int) {
defer wg.Done()
@ -108,7 +108,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
var wg sync.WaitGroup
// These requests will be processed normally until they finish.
for i := 0; i < 50; i++ {
for i := range 50 {
wg.Add(1)
go func(i int) {
defer wg.Done()
@ -123,7 +123,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
// These requests will wait for the first batch to complete but it will take
// too much time, so they will eventually receive a timeout error.
for i := 0; i < 50; i++ {
for i := range 50 {
wg.Add(1)
go func(i int) {
defer wg.Done()
@ -161,7 +161,7 @@ func TestThrottleMaximum(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
for i := range 20 {
wg.Add(1)
go func(i int) {
defer wg.Done()
@ -181,7 +181,7 @@ func TestThrottleMaximum(t *testing.T) {
// At this point the server is still processing, all the following request
// will be beyond the server capacity.
for i := 0; i < 20; i++ {
for i := range 20 {
wg.Add(1)
go func(i int) {
defer wg.Done()
@ -283,7 +283,7 @@ func TestThrottleCustomStatusCode(t *testing.T) {
codes := make(chan int, totalRequestCount)
errs := make(chan error, totalRequestCount)
client := &http.Client{Timeout: timeout}
for i := 0; i < totalRequestCount; i++ {
for range totalRequestCount {
go func() {
resp, err := client.Get(server.URL)
if err != nil {
@ -305,7 +305,7 @@ func TestThrottleCustomStatusCode(t *testing.T) {
}
}
for i := 0; i < totalRequestCount-1; i++ {
for range totalRequestCount - 1 {
waitResponse(http.StatusServiceUnavailable)
}
close(wait) // Allow the last request to proceed.

6
mux.go
View file

@ -467,8 +467,10 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {
// Find the route
if _, _, h := mx.tree.FindRoute(rctx, method, routePath); h != nil {
if supportsPathValue {
setPathValue(rctx, r)
// Set http.Request path values from our request context
for i, key := range rctx.URLParams.Keys {
value := rctx.URLParams.Values[i]
r.SetPathValue(key, value)
}
if supportsPattern {
setPattern(rctx, r)

View file

@ -1693,11 +1693,11 @@ func TestMuxContextIsThreadSafe(t *testing.T) {
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
for range 100 {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 10000; j++ {
for range 10000 {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/ok", nil)
if err != nil {

View file

@ -1,21 +0,0 @@
//go:build go1.22 && !tinygo
// +build go1.22,!tinygo
package chi
import "net/http"
// supportsPathValue is true if the Go version is 1.22 and above.
//
// If this is true, `net/http.Request` has methods `SetPathValue` and `PathValue`.
const supportsPathValue = true
// setPathValue sets the path values in the Request value
// based on the provided request context.
func setPathValue(rctx *Context, r *http.Request) {
for i, key := range rctx.URLParams.Keys {
value := rctx.URLParams.Values[i]
r.SetPathValue(key, value)
}
}

View file

@ -1,19 +0,0 @@
//go:build !go1.22 || tinygo
// +build !go1.22 tinygo
package chi
import "net/http"
// supportsPathValue is true if the Go version is 1.22 and above.
//
// If this is true, `net/http.Request` has methods `SetPathValue` and `PathValue`.
const supportsPathValue = false
// setPathValue sets the path values in the Request value
// based on the provided request context.
//
// setPathValue is only supported in Go 1.22 and above so
// this is just a blank function so that it compiles.
func setPathValue(rctx *Context, r *http.Request) {
}

View file

@ -1,6 +1,3 @@
//go:build go1.22 && !tinygo
// +build go1.22,!tinygo
package chi
import (

20
tree.go
View file

@ -328,7 +328,7 @@ func (n *node) replaceChild(label, tail byte, child *node) {
func (n *node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *node {
nds := n.children[ntyp]
for i := 0; i < len(nds); i++ {
for i := range nds {
if nds[i].label == label && nds[i].tail == tail {
if ntyp == ntRegexp && nds[i].prefix != prefix {
continue
@ -429,9 +429,7 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
}
// serially loop through each node grouped by the tail delimiter
for idx := 0; idx < len(nds); idx++ {
xn = nds[idx]
for _, xn = range nds {
// label for param nodes is the delimiter byte
p := strings.IndexByte(xsearch, xn.tail)
@ -770,20 +768,14 @@ func patParamKeys(pattern string) []string {
}
}
// longestPrefix finds the length of the shared prefix
// of two strings
func longestPrefix(k1, k2 string) int {
max := len(k1)
if l := len(k2); l < max {
max = l
}
var i int
for i = 0; i < max; i++ {
// longestPrefix finds the length of the shared prefix of two strings
func longestPrefix(k1, k2 string) (i int) {
for i = 0; i < min(len(k1), len(k2)); i++ {
if k1[i] != k2[i] {
break
}
}
return i
return
}
type nodes []*node