英文:
Go gota Dataframe not loading struct, requires []struct
问题
我正在阅读一个文件,每一行都包含两个由空格分隔的单词。我扫描并拆分每一行,将两个单词(字符串)存储在MyEntity
结构体(在items
中)。然后将这些单词添加到items
中。
type Entity struct {
Name string
Entity string
}
type MyEntity struct {
Entities []Entity
}
func (entity *MyEntity) AddEntity(item Entity) []Entity {
entity.Entities = append(entity.Entities, item)
return entity.Entities
}
...
items := MyEntity{}
// 在这里循环遍历行 - 名称是第一个单词,实体是第二个单词
item := Entity{
name, entity,
}
items.AddEntity(item)
...
这里的items
不应该是[]struct
吗?我正在使用gota
包(https://github.com/go-gota/gota)从items
创建一个DataFrame,但是使用dataframe.LoadStructs(items)
失败,因为items
是一个struct
,而它必须是一个[]struct
。
我对Go还比较新手,所以我在这里漏掉了什么?
英文:
I am reading a file with each row containing two words separated by a space. I scan and split each line to two words (strings) and store them in the MyEntity
struct (in items
). The words are then added to items
.
type Entity struct {
Name string
Entity string
}
type MyEntity struct {
Entities []Entity
}
func (entity *MyEntity) AddEntity(item Entity) []Entity {
entity.Entities = append(entity.Entities, item)
return entity.Entities
}
...
items := MyEntity{}
// loop here over the rows - name is first word, entity is second word
item := Entity{
name, entity,
}
items.AddEntity(item)
...
Should items
not be a []struct
here? I am using the gota
package (https://github.com/go-gota/gota) to create a DataFrame from items
but using dataframe.LoadStructs(items)
fails because items
is a struct
and it must be a []struct
.
I am fairly new to Go, so what am I missing here?
答案1
得分: 1
从实体切片加载数据框:
df := dataframe.LoadStructs(items.Entities)
英文:
Load the data frame from the slice of entities:
df := dataframe.LoadStructs(items.Entities)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论