This is a creation in Article, where the information may have evolved or changed.
Simple setup of the HTTP server for the Go language
Import the Net/http package first
Import(
"Net/http"
)
Then add 2 lines of code to it.
FuncMain (){
h: =http. Fileserver (http. Dir ("."))
http. Listenandserve (": 8001",h)//Start monitoring
}
What does the above code mean?
http. The parameter inside the Fileserver function is the file address of the Web root directory, which is set to the currently started directory.
The program will then automatically look for the index under this directory. HTML file to be accessed by the client. As shown in.
There is also a way to return information to the client by setting its own content. As follows
FuncHello (whttp. Responsewriter,req*http. Request){
W.write ([]byte ('
<body>
<p>hello world!</p>
</body>
`))
}
http. Handlefunc ("/",Hello)
http. Listenandserve (": 8001",nil)//Start monitoring
Set to nil here