设为首页 加入收藏

TOP

go语言写http踩得坑
2023-07-23 13:33:34 】 浏览:133
Tags:http

1.在运行http时,报错:panic: listen tcp: address xxxx: missing port in address,

  • 初始 代码如下
func HelloWordHander(w http.ResponseWriter, r *http.Request) {
	/**
	具体看一下http协议
	*/
	fmt.Printf("request method: %s\n", r.Method)
	fmt.Printf("request host: %s\n", r.Host)
	fmt.Printf("request url: %s\n", r.URL)
	fmt.Printf("request proto: %s\n", r.Proto)
	fmt.Println("request header")
}
func main() {
	// 上面的HelloWordHander是一个
	http.HandleFunc("/", HelloWordHander)                     // 路由与视图函数作匹配
	if err := http.ListenAndServe("11111", nil); err != nil { //ListenAndServe如果不发生error会一直阻塞。为每一个请求单独创建一个协程去处理
		panic(err)
	}
}

// 然后一运行,就报错:panic: listen tcp: address 11111: missing port in address

  • 解决方法:就是http.ListenAndServe("11111", nil)里面端口(也就是第一个参数)少写了一个 符号 :
// 更改后代码如下,更改后就能正常运行了
func main() {
	// 上面的HelloWordHander是一个
	http.HandleFunc("/", HelloWordHander)                     // 路由与视图函数作匹配
	if err := http.ListenAndServe(":11111", nil); err != nil { //ListenAndServe如果不发生error会一直阻塞。为每一个请求单独创建一个协程去处理
		panic(err)
	}
}

后续采坑会继续添加内容

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇为什么 Go 不支持 []T 转换为 []i.. 下一篇Asynq 实现 Go 异步任务处理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目