我有一个HTTP服务器,我想使用套接字golang http服务器发送r.URL.Path在插座
我得到一个错误发送r.URL.Path文本到客户端:未定义:康恩在conn.Write 这是becauase康恩在另一个函数
定义我曾尝试:
package main
import (
"net"
"io"
"net/http"
)
ln, _ := net.Listen("tcp", ":8081")
conn, _ := ln.Accept()
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
conn.Write([]byte(r.URL.Path + "\n")) //Here I'm attemping to send it
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8000", nil)
}
你得到任何错误? – grooveplex
undefined:conn.Write @grooveplex – rtgojtr
函数'hello'不能引用'main'中的局部变量'conn'。也许你想要一个包级变量。 –