Skip to content

Commit a52c582

Browse files
authored
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.
1 parent 9b9fb55 commit a52c582

11 files changed

Lines changed: 24 additions & 79 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020

2121
strategy:
2222
matrix:
23-
go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.x, 1.24.x]
23+
go-version: [1.22.x, 1.23.x, 1.24.x, 1.25.x]
2424
os: [ubuntu-latest, windows-latest]
2525

2626
runs-on: ${{ matrix.os }}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/go-chi/chi/v5
22

33
// Chi supports the four most recent major versions of Go.
44
// See https://github.com/go-chi/chi/issues/963.
5-
go 1.20
5+
go 1.22

middleware/compress_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ func TestCompressor(t *testing.T) {
9696
}
9797

9898
for _, tc := range tests {
99-
tc := tc
10099
t.Run(tc.name, func(t *testing.T) {
101100
resp, respString := testRequestWithAcceptedEncodings(t, ts, "GET", tc.path, tc.acceptedEncodings...)
102101
if respString != "textstring" {

middleware/content_charset.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middleware
22

33
import (
44
"net/http"
5+
"slices"
56
"strings"
67
)
78

@@ -29,13 +30,7 @@ func contentEncoding(ce string, charsets ...string) bool {
2930
_, ce = split(strings.ToLower(ce), ";")
3031
_, ce = split(ce, "charset=")
3132
ce, _ = split(ce, ";")
32-
for _, c := range charsets {
33-
if ce == c {
34-
return true
35-
}
36-
}
37-
38-
return false
33+
return slices.Contains(charsets, ce)
3934
}
4035

4136
// Split a string in two parts, cleaning any whitespace.

middleware/throttle_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestThrottleBacklog(t *testing.T) {
3737
// The throttler processes 10 consecutive requests, each one of those
3838
// requests lasts 1s. The maximum number of requests this can possible serve
3939
// before the clients time out (5s) is 40.
40-
for i := 0; i < 40; i++ {
40+
for i := range 40 {
4141
wg.Add(1)
4242
go func(i int) {
4343
defer wg.Done()
@@ -75,7 +75,7 @@ func TestThrottleClientTimeout(t *testing.T) {
7575

7676
var wg sync.WaitGroup
7777

78-
for i := 0; i < 10; i++ {
78+
for i := range 10 {
7979
wg.Add(1)
8080
go func(i int) {
8181
defer wg.Done()
@@ -108,7 +108,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
108108
var wg sync.WaitGroup
109109

110110
// These requests will be processed normally until they finish.
111-
for i := 0; i < 50; i++ {
111+
for i := range 50 {
112112
wg.Add(1)
113113
go func(i int) {
114114
defer wg.Done()
@@ -123,7 +123,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
123123

124124
// These requests will wait for the first batch to complete but it will take
125125
// too much time, so they will eventually receive a timeout error.
126-
for i := 0; i < 50; i++ {
126+
for i := range 50 {
127127
wg.Add(1)
128128
go func(i int) {
129129
defer wg.Done()
@@ -161,7 +161,7 @@ func TestThrottleMaximum(t *testing.T) {
161161

162162
var wg sync.WaitGroup
163163

164-
for i := 0; i < 20; i++ {
164+
for i := range 20 {
165165
wg.Add(1)
166166
go func(i int) {
167167
defer wg.Done()
@@ -181,7 +181,7 @@ func TestThrottleMaximum(t *testing.T) {
181181

182182
// At this point the server is still processing, all the following request
183183
// will be beyond the server capacity.
184-
for i := 0; i < 20; i++ {
184+
for i := range 20 {
185185
wg.Add(1)
186186
go func(i int) {
187187
defer wg.Done()
@@ -283,7 +283,7 @@ func TestThrottleCustomStatusCode(t *testing.T) {
283283
codes := make(chan int, totalRequestCount)
284284
errs := make(chan error, totalRequestCount)
285285
client := &http.Client{Timeout: timeout}
286-
for i := 0; i < totalRequestCount; i++ {
286+
for range totalRequestCount {
287287
go func() {
288288
resp, err := client.Get(server.URL)
289289
if err != nil {
@@ -305,7 +305,7 @@ func TestThrottleCustomStatusCode(t *testing.T) {
305305
}
306306
}
307307

308-
for i := 0; i < totalRequestCount-1; i++ {
308+
for range totalRequestCount - 1 {
309309
waitResponse(http.StatusServiceUnavailable)
310310
}
311311
close(wait) // Allow the last request to proceed.

mux.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,10 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {
467467

468468
// Find the route
469469
if _, _, h := mx.tree.FindRoute(rctx, method, routePath); h != nil {
470-
if supportsPathValue {
471-
setPathValue(rctx, r)
470+
// Set http.Request path values from our request context
471+
for i, key := range rctx.URLParams.Keys {
472+
value := rctx.URLParams.Values[i]
473+
r.SetPathValue(key, value)
472474
}
473475
if supportsPattern {
474476
setPattern(rctx, r)

mux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,11 +1693,11 @@ func TestMuxContextIsThreadSafe(t *testing.T) {
16931693

16941694
wg := sync.WaitGroup{}
16951695

1696-
for i := 0; i < 100; i++ {
1696+
for range 100 {
16971697
wg.Add(1)
16981698
go func() {
16991699
defer wg.Done()
1700-
for j := 0; j < 10000; j++ {
1700+
for range 10000 {
17011701
w := httptest.NewRecorder()
17021702
r, err := http.NewRequest("GET", "/ok", nil)
17031703
if err != nil {

path_value.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

path_value_fallback.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

path_value_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build go1.22 && !tinygo
2-
// +build go1.22,!tinygo
3-
41
package chi
52

63
import (

0 commit comments

Comments
 (0)