Home > other >  Golang how to load a local HTML file?
Golang how to load a local HTML file?

Time:10-07

Golang inside, how to use http://localhost: 8000 and load out, I do local HTML file?

CodePudding user response:

Write a handleFunc method, can read your HTML file, and HTTP ResponseWriter write directly read the contents of the

CodePudding user response:

reference 1st floor qybao response:
write a handleFunc method, can read your HTML file, and HTTP ResponseWriter write directly read the contents of the

Well well, this I know, is that the static load the HTML file, how do you write the code

CodePudding user response:

Is common to read file
For example
 import (
"IO/ioutil
""FMT"
)
Func main () {
S, e:=ioutil ReadFile ("/your_path/your_html ")
If e! Nil={
Panic (e)
}
FMT. Println (string (s))
}

CodePudding user response:

Brother handleFunc way, read my HTML files, and HTTP ResponseWriter directly write the content of the reading

Plus you that

What is the complete code?

CodePudding user response:

The
reference 3 floor qybao response:
is common to read file
For example
 import (
"IO/ioutil
""FMT"
)
Func main () {
S, e:=ioutil ReadFile ("/your_path/your_html ")
If e! Nil={
Panic (e)
}
FMT. Println (string (s))
}


Brother handleFunc way, read my HTML files, and HTTP ResponseWriter directly write the content of the reading

Plus you that

What is the complete code?

CodePudding user response:

Halo, this is conveniently came to code, otherwise also too lazy to write
The following is a complete code, can directly reach you need
 import (
"Net/HTTP
""IO/ioutil
""FMT"
)
Func main () {
Mux:=HTTP NewServeMux ()
Mux. HandleFunc ("/", the func (w HTTP ResponseWriter, the req. * the HTTP Request) {
S, e:=ioutil ReadFile ("/your_path/your_html ")//this is to read your HTML
If e! Nil={
Panic (e)
}
FMT. Fprintf (w, string (s))//this is the response
})
Server:=& amp; . The HTTP Server {
Addr: ":" 8080,//monitor server port
Handler: mux,
}
If e:=server. ListenAndServe (); E!=HTTP. ErrServerClosed {
FMT. Printf (" the error was happened: % v. "e)
}
FMT. Println (" server was shut down. ")
}

CodePudding user response:

refer to 6th floor qybao response:
, this is conveniently came to code, otherwise also too lazy to write
The following is a complete code, can directly reach you need
 import (
"Net/HTTP
""IO/ioutil
""FMT"
)
Func main () {
Mux:=HTTP NewServeMux ()
Mux. HandleFunc ("/", the func (w HTTP ResponseWriter, the req. * the HTTP Request) {
S, e:=ioutil ReadFile ("/your_path/your_html ")//this is to read your HTML
If e! Nil={
Panic (e)
}
FMT. Fprintf (w, string (s))//this is the response
})
Server:=& amp; . The HTTP Server {
Addr: ":" 8080,//monitor server port
Handler: mux,
}
If e:=server. ListenAndServe (); E!=HTTP. ErrServerClosed {
FMT. Printf (" the error was happened: % v. "e)
}
FMT. Println (" server was shut down. ")
}

Ha ha a small white sprout new thanked bosses here can still short of a load CSS and js

CodePudding user response:

Demand more and more, with the template, a Google baidu himself how to use the HTML/template

CodePudding user response:

With gin, very convenient

CodePudding user response:

Web development with beego or gin web development frameworks, you said these are all very convenient, if use the native, very troublesome, you have to own design routing framework, mux. HandleFunc ("/show "like that, you can only be used when the browser to access to visit http://localhost:8080/show, http://localhost:8080/show, http://localhost:8080/show... Cannot access, you have to from already do mux. HandleFunc ("/", httprouterhandle)

Func httprouterhandle (w HTTP ResponseWriter, the req. * the HTTP Request) {
Path:=ToLower (the req. URL. Path)
The switch path {
Case "/show" :
Adjust your corresponding method
Default:
Illegal URL

}
}

Thank qybao 8 floor, I mentioned this problem, is to provide solutions
heIf your website's URL routing is very much, so httprouterhandle case judgment is very much, that have to write from already registered the function of the routing table, similar beego such code:
Beego. The Router ("/pay/gamepay. HTML ", & amp; Controllers. GetGamePageStruct {}, "get: GetGamePage)," said for/pay/gamepay HTML URL request like this, get access to map to & amp; Controllers. GetGamePageStruct {}, GetGamePageStruct structure method, and then in your path traversal httprouterhandle registered routing, find ways to jump into the corresponding registered in execution, can't find so illegal URL error,
So make trouble, use beego, gin web framework, such as convenient,

CodePudding user response:

Native load HTML files use template, like this
Func showLoginPage (w HTTP ResponseWriter, r * HTTP Request) {
If r.M ethod=="GET" {
T, _ :=template. ParseFiles (" goweb/form/login. HTML ")
T.E xecute (w, nil)
}
}
Detailed yourself to learn the function of the template, there is only the most simple template examples

CodePudding user response:

If you advise look at basic knowledge, learning, and then learn the popular web framework,
If you only use, directly with the popular web framework,
  • Related