Go意外的字符串字面量

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

Go unexpected string literal

问题

这是我的Go代码,我想一切都是正确的....

package main

import (
	"fmt"
	"encoding/json"
	"net/http"
)

type Payload struct {
	Stuff Data
}

type Data struct {
	Fruit    Fruits
	Veggies  Vegetables
}

type Fruits map[string]int
type Vegetables map[string]int

func serveRest(w http.ResponseWriter, r *http.Request) {
	response, err := getJsonResponse()
	if err != nil {
		panic(err)
	}
	fmt.Fprintln(w, string(response))
}

func main() {
	http.HandleFunc("/", serveRest)
	http.ListenAndServe("localhost:1337", nil)
}

func getJsonResponse() ([]byte, error) {
	fruits := make(map[string]int)
	fruits["Apples"] = 25
	fruits["Oranges"] = 11

	vegetables := make(map[string]int)
	vegetables["Carrots"] = 21
	vegetables["Peppers"] = 0

	d := Data{fruits, vegetables}
	p := Payload{d}

	return json.MarshalIndent(p, "", "  ")
}

这是我得到的错误信息:

API_Sushant.go:31: 语法错误:意外的字符串文字,期望分号、换行符或 }
有人能告诉我错误是什么吗?

希望能帮到你!

英文:

This is my code in Go and everything is right I guess....

package main

import(
"fmt"
"encoding/json"
"net/http"

)
type Payload struct {
	Stuff Data
}
type Data struct {
	Fruit Fruits
	Veggies Vegetables
}
type Fruits map[string]int
type Vegetables map[string]int


func serveRest(w http.ResponseWriter, r *httpRequest){
	response , err := getJsonResponse()
	if err != nil{
		panic(err)
	}
	fmt.println(w, string(response))

}






func main(){

http.HandleFucn("/", serveRest)
http.ListenAndServe("localhost:1337",nil)
}


func getJsonResponse() ([]byte, error){

fruits := make(map[string]int)
fruits["Apples"] = 25
fruits["Oranges"] = 11

vegetables := make(map[string]int)
vegetables["Carrots"] = 21
vegetables["Peppers"] = 0

d := Data{fruits, vegetables}
p := Payload{d}

return json.MarshalIndent(p, "", "  ")

}

And this is the error I am getting

API_Sushant.go:31: syntax error: unexpected string literal, expecting semicolon or newline or }

Can anybody tell me whats the error plz ....

答案1

得分: 2

你的示例中有一些小的拼写错误。在修复这些错误后,你的示例在我这里运行时没有出现“unexpected string literal”错误。另外,如果你想将JSON写入http.ResponseWriter,你应该将fmt.Println更改为fmt.Fprintln,如下面的第2部分所示。

(1)小的拼写错误

// 错误1:undefined: httpRequest
func serveRest(w http.ResponseWriter, r *httpRequest){
// 修正:
func serveRest(w http.ResponseWriter, r *http.Request){

// 错误2:cannot refer to unexported name fmt.println
fmt.println(w, string(response))
// 修正以消除错误。使用Fprintln来写入到 'w' http.ResponseWriter
fmt.Println(w, string(response))

// 错误3:undefined: http.HandleFucn
http.HandleFucn("/", serveRest)
// 修正
http.HandleFunc("/", serveRest)

(2)在HTTP响应中返回JSON

由于fmt.Println写入标准输出,而fmt.Fprintln写入提供的io.Writer,要在HTTP响应中返回JSON,请使用以下代码:

fmt.Fprintln(w, string(response))
英文:

There are a few minor typos in your example. After fixing those, your example ran for me without the unexpected string literal error. Also, if you want to write the JSON to the http.ResponseWriter, you should change fmt.Println to fmt.Fprintln as shown in part 2 below.

(1) Minor Typos

// Error 1: undefined: httpRequest
func serveRest(w http.ResponseWriter, r *httpRequest){
// Fixed:
func serveRest(w http.ResponseWriter, r *http.Request){

// Error 2: cannot refer to unexported name fmt.println
fmt.println(w, string(response))
// Fixed to remove error. Use Fprintln to write to 'w' http.ResponseWriter
fmt.Println(w, string(response))

// Error 3: undefined: http.HandleFucn
http.HandleFucn("/", serveRest)
// Fixed
http.HandleFunc("/", serveRest)

(2) Returning JSON in HTTP Response

Because fmt.Println writes to standard output and fmt.Fprintln writes to supplied io.Writer, to return the JSON in the HTTP response, use the following:

fmt.Fprintln(w, string(response))

huangapple
  • 本文由 发表于 2015年3月22日 04:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/29187854.html
匿名

发表评论

匿名网友

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

确定