Golang http.Get()
我正在尝试编写一个基本的http服务器示例。我可以从客户端脚本中获取Golang http.Get(),http,go,server,Http,Go,Server,我正在尝试编写一个基本的http服务器示例。我可以从客户端脚本中获取localhost:8080,但无法使用http.Get(“127.0.0.1:8080”)与服务器联系。我做错了什么 server.go: import "fmt" import "net/http" func join(w http.ResponseWriter, req *http.Request) { fmt.Println("Someone joined") } func main() { fmt.
localhost:8080
,但无法使用http.Get(“127.0.0.1:8080”)
与服务器联系。我做错了什么
server.go:
import "fmt"
import "net/http"
func join(w http.ResponseWriter, req *http.Request) {
fmt.Println("Someone joined")
}
func main() {
fmt.Println("Listening on port 8080...")
http.HandleFunc("/join", join)
http.ListenAndServe(":8080", nil)
}
client.go:
import "net/http"
http.Get("127.0.0.1:8080/join")
尝试http.Get(“http://127.0.0.1:8080/join)
。请注意“http:”。此外,请检查错误。它会告诉你问题出在哪里
resp, err := http.Get("http://127.0.0.1:8080/join")
if err != nil {
log.Fatal(err)
}
您尚未指定方案,请尝试
http.Get(“http://127.0.0.1:8080/join“”
http.Get
与许多其他go函数一样返回错误,因此,如果您编写了自己喜欢的代码:
_, err := http.Get("127.0.0.1:8080/join")
if err != nil{
fmt.Println(err)
}
你会看到:
Get 127.0.0.1:8080/join: unsupported protocol scheme ""
“我[…]无法联系服务器”并不是对您面临的实际问题的有用描述。顺便说一句:捕获并处理http.Get中的错误将在这里提供信息。嘿,如果我不想在代码中使用
err
,我如何替换resp,err:=http.Get(“http://127.0.0.1:8080/join“
带有类似于response\u struct:=http.Get(”http://127.0.0.1:8080/join”
?