Skip to content

net/http: precedence in dynamic urls doesn't work as expected when using http.ServeMux #68030

Closed as not planned
@covicale

Description

@covicale

Go version

go1.22.4 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/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.22.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build744119443=/tmp/go-build -gno-record-gcc-switches'

What did you do?

// main.go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func staticURLHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("This is a static url"))
}

func dynamicURLHandler(w http.ResponseWriter, r *http.Request) {
	pathVariable := r.PathValue("example")
	text := fmt.Sprintf("This is a dynamic url with path: %v", pathVariable)
	w.Write([]byte(text))
}

func ExampleMux() http.Handler {
	exampleMux := http.NewServeMux()
	exampleMux.HandleFunc("/test", staticURLHandler)
	return exampleMux
}

func main() {
	server := http.NewServeMux()
	port := ":8080"

	exampleMux := ExampleMux()
	server.HandleFunc("/static", staticURLHandler)
	server.Handle("/", exampleMux)
	server.HandleFunc("/{example}", dynamicURLHandler)

	log.Printf("App runing on localhost%v\n", port)
	http.ListenAndServe(port, server)
}

go run main.go

What did you see happen?

Accessing /static
image
Accessing a dynamic url with {example}
image
Accessing /test
image

What did you expect to see?

By the precedence rules, if I access /test, it should use the static URL handler, but the dynamic URL handler is used. This behavior likely has to do with the use of multiplexers. It is intentional?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions