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

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行