go: var声明但未使用错误 – 如何解决它?

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

go: var declared but not used error - how to work around it?

问题

在这个函数中,我得到了一个“声明但未使用”的错误,我不明白 - 我需要以某种方式标记它为“我确实使用了它”吗?

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, "它不是一个字符串"}
}

奇怪的是,我没有从这个函数中得到错误:

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, "它不是一个整数"}
}

此外,对于将它们合并为一个函数的正确方法,您有什么想法?

英文:

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!

答案1

得分: 1

你的错误来自于在其他地方声明和不使用相同的标识符,因为这段代码在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
}
英文:

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
}

答案2

得分: 0

你的代码看起来是正确的,错误无法重现。
当然,你可以将这些代码重构为一个单一的函数,但根据个人喜好可能会有所不同。

type VType int
const (
    VInteger VType = iota
    VString
    VUnknown
)
func getValue(data map[string]interface{}, name string) (VType, int, string) {
    switch v := data[name].(type) {
    case int:
        return VInteger, v, ""
    case string:
        return VString, 0, v
    default:
        return VUnknown, 0, ""
    }
}
func main() {
    m := make(map[string]interface{})
    m["foo"] = "25"
    switch t, i, s := getValue(m, "foo"); t {
    case VInteger:
        fmt.Println("int ", i)  //do something with int
    case VString:
        fmt.Println("string ", s) //do something with string
    case VUnknown:
        err := &apiError{1, "it's not an int"} //do something with err
    }
}

希望对你有帮助!

英文:

Your code looks correct, error isn't reproducible.
Sure you can refactor these into a single function, but you may not like it depending of tastes.

type VType int
const (
	VInteger VType = iota
	VString
	VUnknown
)
func getValue(data map[string]interface{}, name string) (VType, int, string) {
	switch v := data[name].(type) {
	case int:
		return VInteger, v, ""
	case string:
		return VString, 0, v
	default:
		return VUnknown, 0, ""
	}
}
func main() {
	m := make(map[string]interface{})
	m["foo"] = "25"
	switch t, i, s := getValue(m, "foo"); t {
	case VInteger:
		fmt.Println("int ", i)  //do something with int
	case VString:
		fmt.Println("string ", s) //do something with string
	case VUnknown:
		err := &apiError{1, "it's not an int"} //do something with err
	}
}

huangapple
  • 本文由 发表于 2016年9月9日 06:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/39401185.html
匿名

发表评论

匿名网友

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

确定