dongwei2983 2016-09-08 22:35
浏览 38

转到:var声明但未使用错误-如何解决?

In this function I get "s declared and not used" which I don't understand - do I need to somehow tag it as 'really I used it' or something?

func getString(data map[string]interface{}, name string) (string, error) {
    s := data[name]
    if reflect.TypeOf(s).Kind() != reflect.String {
        return s.(string), nil
    }
    return "", &apiError{1, "it's not a string"}
}

Oddly, I don't get the error from this function:

func getInt(data map[string]interface{}, name string) (int, error) {
    t := data[name]
    if reflect.TypeOf(t).Kind() == reflect.Int {
        return t.(int), nil
    }
    return 0, &apiError{1, "it's not an int"}
}

Also, any thoughts on the right way to factor these into a single function would be welcomed!

  • 写回答

2条回答 默认 最新

  • dongzhuo3202 2016-09-08 22:48
    关注

    Your error comes from (declaring and not) using the same identifier elsewhere because this compiles and runs fine on golang.org:

    package main
    
    import "reflect"
    
    func main() {
        m := make(map[string]interface{})
        m["foo"] = "25"
        getString(m, "foo")
    }
    
    func getString(data map[string]interface{}, name string) (string, error) {
        s := data[name]
        if reflect.TypeOf(s).Kind() != reflect.String {
            return s.(string), nil
        }
        return "", nil
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题