我正在编写一个简单的服务器作为一个项目来帮助我学习 Go。这是它的最小形式:
package main
import (
"io"
"net/http"
)
func serve(w http.ResponseWriter, r *http.Request) {
text := "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Host: localhost:1234\r\n" +
"Connection: close\r\n" +
"\r\n" +
"<!doctype html>\r\n" +
"<html>...</html>\r\n"
// Greet the user, ignoring their request.
io.WriteString(w, text)
}
func main() {
http.HandleFunc("/", serve)
http.ListenAndServe(":1234", nil)
}
当客户端连接到localhost:1234. 但出于某种原因,我的浏览器根据需要将输出显示为文本而不是 HTML。我究竟做错了什么?
呼唤远方
qq_遁去的一_1
相关分类