Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
johnpeeke opened this issue Sep 27, 2024 · 2 comments

Comments

@johnpeeke
Copy link

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.

@ianlancetaylor
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants