Go无法加载结构体,需要使用[]struct。

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

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)

huangapple
  • 本文由 发表于 2022年1月20日 15:05:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/70781996.html
匿名

发表评论

匿名网友

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

确定