HTTP method before the endpoint

205 views
Skip to first unread message

rogerjd

unread,
Jun 2, 2024, 5:26:36 PM6/2/24
to golang-nuts
Routing Enhancements for Go 1.22

I have this in the init func for main
    http.HandleFunc("GET /posts", handlePost2)

Yet, in the browser, when http://127.0.0.1/posts is entered, it returns
404 page not found.

Am I doing something wrong? Maybe the problem lies elsewhere?
Thank you,
Roger

func handlePost2(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "GET test handlePost2")
}

p...@morth.org

unread,
Jun 3, 2024, 3:26:22 AM6/3/24
to golang-nuts
Hi,

It works on the playground at least, https://go.dev/play/p/YZbgmezjSIV
Are you sure your server is listening on port 80? That's fairly uncommon for service code and usually requires extra permissions.
If it's listening on another port, you'll have to specify that in the browser as well (see line 27 in the link above).

Regards,
Per Johansson

rogerjd

unread,
Jun 3, 2024, 1:20:20 PM6/3/24
to golang-nuts
Your code works both in the playground and on my local machine.
I need to figure what I did wrong originally.
Thank you for helping,
Roger

rogerjd

unread,
Jun 3, 2024, 4:51:23 PM6/3/24
to golang-nuts
If it helps someone else, :
The problem was it was using go version 1.20
Needed to change go.mod to use
go 1.22.3


Reply all
Reply to author
Forward
0 new messages