Skip to content

net/http: enhanced ServerMux routing #65231

Closed
@kassner

Description

@kassner

Go version

go1.22rc1

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/home/kassner/.cache/go-build'
GOENV='/home/kassner/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/kassner/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/kassner/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/kassner/sdk/go1.22rc1'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/kassner/sdk/go1.22rc1/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.22rc1'
GCCGO='gccgo'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/kassner/workspace/go122test/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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1457264763=/tmp/go-build -gno-record-gcc-switches'

What did you do?

package main

import (
	"fmt"
	"io"
	"net/http"
	"sync"
	"time"
)

func main() {
	handler := http.NewServeMux()
	handler.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(r.URL.Path))
	})

	go http.ListenAndServe(":9090", handler)

	wg := sync.WaitGroup{}
	wg.Add(1)

	go func() {
		defer wg.Done()
		time.Sleep(time.Second)

		resp, err := http.Get("http://localhost:9090/")
		if err != nil {
			panic(err)
		}

		body, _ := io.ReadAll(resp.Body)
		if err != nil {
			panic(err)
		}

		fmt.Println("statusCode:", resp.StatusCode)
		fmt.Println("body:", string(body))
	}()

	wg.Wait()
}

What did you see happen?

statusCode: 404
body: 404 page not found

What did you expect to see?

statusCode: 200
body: /

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions