golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.14k stars 17.56k forks source link

net/http.ServeMux: Enhanced routing patterns introduced in 1.22 do not work in 1.23 unless go mod has go version explicitly declared #69686

Closed johnpeeke closed 44 minutes ago

johnpeeke commented 2 hours ago

Go version

go version 1.23

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jpeeke/.cache/go-build'
GOENV='/home/jpeeke/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jpeeke/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jpeeke/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/jpeeke/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='0'
GOMOD='/mnt/c/Users/jpeeke/awesomeProject1/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2766611647=/tmp/go-build -gno-record-gcc-switches'

What did you do?

`func main() { apiCfg := &apiConfig{} apiCfg.fileserverHits = atomic.Int32{}

mux := http.NewServeMux()
mux.Handle("/app/", apiCfg.middlwareMetricsInc(http.StripPrefix("/app", http.FileServer(http.Dir(".")))))
mux.HandleFunc("GET /healthz", healthCheck)
mux.HandleFunc("GET /metrics", apiCfg.loadHits)
mux.HandleFunc("POST /reset", apiCfg.resetFileserverHits)

srv := &http.Server{
    Addr:    ":8080",
    Handler: mux,
}

log.Println(srv.ListenAndServe())

}`

Navigate to any endpoint using the new enhanced routing pattern introduced in go version 1.22 (i.e. Get /healthz)

go.dev/play

What did you see happen?

I receive a 404 error

What did you expect to see?

I expected an OK response. I receive the correct response only if go version 1.22 or 1.23 is explicitly set in go.mod. I did not expect to have to explicitly set this as only go version 1.23 is installed on my system.

gabyhelp commented 2 hours ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

ianlancetaylor commented 44 minutes ago

If you have a go.mod file, and it does not specify a go version, the default is to assume that you are using an old version of Go, specifically 1.16. For cases like enhanced routing patterns, you will therefore get the behavior of the Go 1.16 standard library. This is working as intended.