Wednesday, March 15, 2023
HomeGolangOffering fixed documents - Code Testimonial

Offering fixed documents – Code Testimonial

I’m having problems with offering fixed documents. Just how i can offer fixed documents?

 bundle primary

import (
" mistakes".
" fmt".
" net/http".
" os".

func getRoot( w http.ResponseWriter, r * http.Request) {
fmt.Printf(" obtained/ requestn").
http.ServeFile( w, r, "index.html");.

func primary() {
if os.Args[1] == "run" {
fixed:= http.FileServer( http.Dir("./")).
http.Handle("", fixed).

http.HandleFunc("", getRoot).

err:= http.ListenAndServe(":3000", nil).

if errors.Is( err, http.ErrServerClosed) {
fmt.Printf(" web server closedn").
} else if err!= nil {
fmt.Printf(" mistake beginning web server: %sn", err).
os.Exit( 1 ).

When I attempt to run your code, I obtain this panic:

 panic: http: numerous enrollments for/.

goroutine 1 [running]:.
net/http.(* ServeMux). Deal With( 0xe112c0, {0xc3e512, 0x1}, {0xcb2040?, 0xc6d040} ).
C:/ Program Files/Go/src/ net/http/server. go:2478 +0 x226.
net/http.(* ServeMux). HandleFunc( ...).
C:/ Program Files/Go/src/ net/http/server. go:2515.
net/http. HandleFunc( ...).
C:/ Program Files/Go/src/ net/http/server. go:2527.
C:/ Users/Sean/go/ src/forum. +0 xcc.
leave standing 2.

The very first line claims panic: http: numerous enrollments for/ and also if I take a look at your code, line 18 signs up the "" course to be taken care of by the fixed feature and also line 20 signs up "" to be taken care of by the getRoot feature. If I:

  1. Remark out either among these features,
  2. Develop an index.html documents,
  3. Execute go run. run
  4. Browse to http://localhost:3000/

I obtain the web content of my index.html documents.

2 Sorts

Simply to include in @skillian‘s solution: providing index.html with your getRoot trainer is repetitive. http.FileServer will appropriately provide index.html when it sees an origin course ( and also as a matter of fact reroutes “/ index.html” to “/”):

As a grandfather clause, the returned documents web server reroutes any kind of demand finishing in “/ index.html” to the very same course, without the last “index.html”.

1 Like

This subject was immediately shut 90 days after the last reply. New replies are no more enabled.


Most Popular

Recent Comments