英文:
Go, Golang : traverse through struct
问题
我想遍历一个结构体数组。
func GetTotalWeight(data_arr []struct) int {
total := 0
for _, elem := range data_arr {
total += elem.weight
}
return total
}
但是我遇到了语法错误:
syntax error: unexpected ), expecting {
是否可以遍历结构体?
英文:
http://play.golang.org/p/fJACxhSrXX
I want to traverse through an array of structs.
func GetTotalWeight(data_arr []struct) int {
total := 0
for _, elem := range data_arr {
total += elem.weight
}
return total
}
But I am getting syntax error
syntax error: unexpected ), expecting {
Is it possible to traverse through structs?
答案1
得分: 18
你的函数几乎完全正确。你想将TrainData定义为一个type
,并将GetTotalWeight
的类型签名更改为[]TrainData
,而不是[]struct
,像这样:
import "fmt"
type TrainData struct {
sentence string
sentiment string
weight int
}
var TrainDataCity = []TrainData{
{"我喜欢这里的天气。", "pos", 1700},
{"这是一个令人惊叹的地方!", "pos", 2000},
{"我对它的食物和氛围感觉非常好。", "pos", 2000},
{"位置非常便利。", "pos", 1500},
{"我去过的最好的城市之一。", "pos", 2000},
{"一定要再次访问。", "pos", 2000},
{"我不喜欢这个地区。", "neg", 500},
{"我对这个城市感到厌倦。", "neg", 700},
{"我再也无法忍受这个城镇了。", "neg", 300},
{"天气太糟糕了。", "neg", 300},
{"我讨厌这个城市。", "neg", 100},
{"我不会再回来了!", "neg", 200},
}
func GetTotalWeight(data_arr []TrainData) int {
total := 0
for _, elem := range data_arr {
total += elem.weight
}
return total
}
func main() {
fmt.Println("Hello, playground")
fmt.Println(GetTotalWeight(TrainDataCity))
}
运行结果为:
Hello, playground
13300
英文:
Your function is almost entirely correct. You want to define TrainData as a type
, and change the type signature of GetTotalWeight
to []TrainData
, not []struct
, like so:
import "fmt"
type TrainData struct {
sentence string
sentiment string
weight int
}
var TrainDataCity = []TrainData {
{"I love the weather here.", "pos", 1700},
{"This is an amazing place!", "pos", 2000},
{"I feel very good about its food and atmosphere.", "pos", 2000},
{"The location is very accessible.", "pos", 1500},
{"One of the best cities I've ever been.", "pos", 2000},
{"Definitely want to visit again.", "pos", 2000},
{"I do not like this area.", "neg", 500},
{"I am tired of this city.", "neg", 700},
{"I can't deal with this town anymore.", "neg", 300},
{"The weather is terrible.", "neg", 300},
{"I hate this city.", "neg", 100},
{"I won't come back!", "neg", 200},
}
func GetTotalWeight(data_arr []TrainData) int {
total := 0
for _, elem := range data_arr {
total += elem.weight
}
return total
}
func main() {
fmt.Println("Hello, playground")
fmt.Println(GetTotalWeight(TrainDataCity))
}
Running this gives:
Hello, playground
13300
答案2
得分: 1
range
关键字仅适用于字符串、数组、切片和通道。因此,无法使用range
迭代结构体。但是,你提供了一个切片,所以这不是个问题。问题在于函数的类型定义。
你写的是:
func GetTotalWeight(data_arr []struct) int
现在问问自己:我在这里请求的是什么类型?
以[]
开头的部分表示切片,所以我们处理的是一个结构体切片。
但是,是什么类型的结构体?唯一匹配所有结构体的方法是使用接口值。否则,你需要给出一个明确的类型,例如TrainData
。
这是一个语法错误的原因是,语言只允许在定义新结构体时使用struct
关键字。结构体定义由struct
关键字开头,后跟{
,这就是编译器告诉你他期望的内容。
结构体定义示例:
a := struct{ a int }{2} // 匿名结构体,有一个成员
英文:
The range
keyword works only on strings, array, slices and channels. So, no it is not possible to iterate over structs with range
. But you supply a slice, so that's not a problem. The problem is the type defenition of the function.
You write:
func GetTotalWeight(data_arr []struct) int
Now ask yourself: what type did I request here?
Everything starting with []
indicates a slice, so we deal with a slice of struct.
But what type of struct? The only way to match ever struct would be to use
interface values. Otherwise you'd need to give an explicit type, for example
TrainData
.
The reason why this is an syntax error is, that the only time the language allows
the struct
keyword is on defining a new struct. A struct definition has the
struct keyword, followed by a {
and that's why the compiler tells you that he expects
the {
.
Example for struct definitions:
a := struct{ a int }{2} // anonymous struct with one member
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论