从Golang http.writerrepsonse开始

m1m5dgzv  于 2024-01-04  发布在  Go
关注(0)|答案(1)|浏览(132)

我刚开始使用golang(已经熟悉django),我试图在浏览器中显示“Hello world!”字符串
第一个月

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func helloWorldPage(w http.ResponseWriter, *http.Request) {
  7. fmt.Fprint(w, "hello world")
  8. }
  9. func main() {
  10. http.HandleFunc("/", helloWorldPage)
  11. //Just sets up a default server :80
  12. http.ListenAndServe(":8000", nil)
  13. }

字符串
我按照教程,并在文档中查找,但我得到这个错误:
混合命名和未命名参数语法var w http.ResponseWriter

pgky5nke

pgky5nke1#

简而言之:第二个参数(即 *http.Request)需要一个名称,或者如果您不想使用它,请使用_
所以我的代码看起来像这样:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func helloWorldPage(w http.ResponseWriter, req *http.Request) {
  7. fmt.Fprint(w, "hello world")
  8. }
  9. func main() {
  10. http.HandleFunc("/", helloWorldPage)
  11. //Just sets up a default server :80
  12. http.ListenAndServe(":8000", nil)
  13. }

字符串
回复:@mkopriva

展开查看全部

相关问题