英文:
cannot use true (type bool) as type string in function argument
问题
我正在尝试从Elasticsearch获取搜索结果,但是我遇到了这个错误:
cannot use true (type bool) as type string in function argument
请问有人可以帮助解决吗?我对Go编程还不熟悉,所以希望能得到改进这个问题的建议。
这是我的代码片段:
func SearchCallback(w *http.ResponseWriter, req *http.Request) {
api.Domain = "127.0.0.1"
// search males
searchQuery := `{
"query": {
"term": {"content":"male"}
}
}`
var response2 map[string]interface{}
response2, err := core.SearchRequest(true, "people", "male", searchQuery, "")
if err != nil {
log.Fatalf("The search of males has failed:", err)
}
var values2 []interface{}
for _, v := range response2.Hits.Hits {
var value2 map[string]interface{}
err := json.Unmarshal(v.Source, &value2)
if err != nil {
log.Fatalf("Failed to unmarshal, line 65", err)
}
values2 = append(values2, value2)
}
fmt.Println(values2)
jsonV2, err := json.Marshal(values2)
if err != nil {
log.Fatalf("Failed marshalling: line 71", err)
}
fmt.Println(string(jsonV2))
}
请注意,我只翻译了代码部分,其他内容不做翻译。
英文:
I'm trying to get Search results from elastic search, but i'm getting this error:
cannot use true (type bool) as type string in function argument
Please can anyone can help to resolve it? I'm new to Go programming, so would appreciate suggestions to improve this case in the future.
This my code snippet
func SearchCallback(w *http.ResponseWriter, req *http.Request) {
api.Domain = "127.0.0.1"
// search males
searchQuery := `{
"query": {
"term": {"content":"male"}
}
}`
var response2 map[string]interface{}
response2, err := core.SearchRequest(true, "people", "male", searchQuery, "")
if err != nil {
log.Fatalf("The search of males has failed:", err)
}
var values2 []interface{}
for _, v := range response2.Hits.Hits {
var value2 map[string]interface{}
err := json.Unmarshal(v.Source, &value2)
if err != nil {
log.Fatalf("Failed to unmarshal, line 65", err)
}
values2 = append(values2, value2)
}
fmt.Println(values2)
jsonV2, err := json.Marshal(values2)
if err != nil {
log.Fatalf("Failed marshalling: line 71", err)
}
fmt.Println(string(jsonV2))
}
答案1
得分: 6
Go语言没有隐式类型转换。简单来说,这意味着一个类型不能被视为另一个类型,除非明确声明要将其视为该类型。int不是float,float不是slice,bool不是string。有时你可以使用类型转换来解决这个问题,但对于布尔值来说,你不能简单地使用类型转换。也就是说,string(bool)
不能编译通过。
有多种方法可以解决这个问题。一种方法是用fmt.Sprintf("%t", true)
替换true
。这是更一般的情况,如果true
是一个布尔变量,它将返回一个字符串,表示为"true"
或"false"
。它也适用于其他类型,比如整数,fmt.Sprintf("%d", myInteger)
可以将int
转换为string
。
然而,在这种情况下,由于值始终为true,这是不必要的,只需将true
替换为"true"
即可。"%t"
将返回字符串"true"
,所以没有必要进行转换的麻烦。
英文:
Go doesn't have implicit type conversion. Since you're new to programming -- what this amounts to is saying that one type cannot be treated as another without explicitly stating that you want it to be treated as such. An int is not a float, a float is not a slice, and a bool is not a string. Sometimes you can use type conversions to fix this, unfortunately, for boolean values you cannot simply use a type conversion. That is, string(bool)
will not compile.
There are multiple ways to fix this. One is to replace true
with fmt.Sprintf("%t",true)
. This is the more general case, if true
was instead a boolean variable it would return a string that says "true"
or "false"
depending. It also generalizes well to other types such as integers, fmt.Sprintf("%d",myInteger)
, for instance, would convert an int
to a string
.
However... in this case since the value is always true, it's needless, just replace true
with "true"
. The string that "%t"
will return will be the string "true"
so there's no need to go through the conversion hassle.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论