创建一个简单的Json响应GO

huangapple go评论107阅读模式
英文:

Creating a simple Json response GO

问题

我正在查看一个使用GO创建REST API的教程。我正在尝试构建一个Web服务器并提供一个简单的JSON响应:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type Payload struct {
  8. Stuff Data
  9. }
  10. type Data struct {
  11. Fruit Fruits
  12. Veggies Vegetables
  13. }
  14. type Fruits map[string]int
  15. type Vegetables map[string]int
  16. func serveRest(w http.ResponseWriter, r *http.Request) {
  17. response, err := getJsonResponse()
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Fprintf(w, string(response))
  22. }
  23. func main() {
  24. http.HandleFunc("/", serveRest)
  25. http.ListenAndServe("localhost:7200", nil)
  26. }
  27. func getJsonResponse() ([]byte, error) {
  28. fruits := make(map[string]int)
  29. fruits["Apples"] = 25
  30. fruits["Oranges"] = 11
  31. vegetables := make(map[string]int)
  32. vegetables["Carrots"] = 21
  33. vegetables["Peppers"] = 0
  34. d := Data{fruits, vegetables}
  35. p := Payload{d}
  36. return json.MarshalIndent(p, "", " ")
  37. }

当我运行这段代码时,我得到以下错误:

  1. too many arguments to conversion to Data: Data(fruits, vegetables)

我不明白为什么会出现这个错误,因为它期望有2个参数,而我正好传入了2个参数。这是我第一天尝试GO,所以可能我漏掉了一些概念或其他东西。

英文:

I'm looking at a tutorial for creating REST API's with GO. I'm trying to build a web server and serve up a simple json response:

  1. package main
  2. import(
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type Payload struct {
  8. Stuff Data
  9. }
  10. type Data struct {
  11. Fruit Fruits
  12. Veggies Vegetables
  13. }
  14. type Fruits map[string]int
  15. type Vegetables map[string]int
  16. func serveRest(w http.ResponseWriter, r *http.Request) {
  17. response, err := getJsonResponse()
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Fprintf(w, string(response))
  22. }
  23. func main() {
  24. http.HandleFunc("/", serveRest)
  25. http.ListenAndServe("localhost:7200", nil)
  26. }
  27. func getJsonResponse() ([]byte, error){
  28. fruits := make(map[string]int)
  29. fruits["Apples"] = 25
  30. fruits["Oranges"] = 11
  31. vegetables := make(map[string]int)
  32. vegetables["Carrots"] = 21
  33. vegetables["Peppers"] = 0
  34. d := Data(fruits, vegetables)
  35. p := Payload(d)
  36. return json.MarshalIndent(p, "", " ")
  37. }

When I run this code I get the error:

  1. too many arguments to conversion to Data: Data(fruits, vegetables)

I don't understand why its throwing that error since its expecting 2 arguments and I'm passing in 2 arguments. This is my first day trying GO so maybe I'm missing some concept or something.

答案1

得分: 3

我发现了我的错误,我传递了一个函数而不是一个对象给我创建的类型:

  1. d := Data(fruits, vegetables)
  2. p := Payload(d)

应该改为:

  1. d := Data{fruits, vegetables}
  2. p := Payload{d}
英文:

I found my error I passed in a function instead of an object to the types I created:

  1. d := Data(fruits, vegetables)
  2. p := Payload(d)

should be:

  1. d := Data{fruits, vegetables}
  2. p := Payload{d}

huangapple
  • 本文由 发表于 2015年8月16日 00:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/32027000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定