gpt4 book ai didi

http - 戈兰 http.Get()

转载 作者:数据小太阳 更新时间:2023-10-29 03:38:13 27 4
gpt4 key购买 nike

我正在尝试编写一个基本的 http 服务器示例。我可以 curl 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)
}

客户端.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 - 戈兰 http.Get(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42679436/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com