dongxi1965 2018-06-11 15:59
浏览 41
已采纳

尝试读取JSON时在函数主体外部显示非声明语句

I'm getting the following two errors on the following code and it won't compile. I'm not really sure what I've done wrong in this code. Here are my errors: .\app.go:52:52: syntax error: unexpected }, expecting comma or ) .\app.go:55:1: syntax error: non-declaration statement outside function body. I don't know what else to check in the following code. Any help would be appreciated

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"
)

type Results struct {
    Results []Result `json:"results"`
}

type Result struct {
    Name   string  `json:"name"`
    Rating float64 `json:"rating"`
}

func main() {
    url := "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=33.985,-118.4695&radius=500&type=restaurant&key=MYKEY"

    resultClient := http.Client{
        Timeout: time.Second * 2,
    }

    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        log.Fatal(err)
    }

    req.Header.Set("User-Agent", "eattheshoals")

    res, getErr := resultClient.Do(req)
    if getErr != nil {
        log.Fatal(getErr)
    }

    byteValue, _ := ioutil.ReadAll(res.Body)
    //if readErr != nil {
    //  log.Fatal(readErr)
    //}

    var results Results
    jsonErr := json.Unmarshal(byteValue, &results)
    if jsonErr != nil {
        log.Fatal(jsonErr)
    }

    for i := 0; i < len(results.Results); i++ {
        fmt.Println("Name " + results.Results[i].Name)
        fmt.Println("Rating " + results.Results[i].Rating})
    }
}
  • 写回答

1条回答 默认 最新

  • douhuo0884 2018-06-11 16:10
    关注

    there is an extra bracer in your last Println

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接