【问题标题】:How to split a struct in Golang according to specific values如何根据特定值拆分Golang中的结构
【发布时间】:2019-03-21 11:14:46
【问题描述】:

我有 Python 和 R 背景,并且习惯于使用数据框。 如果我有下表:

>>> table

ID        Phone   Email Value
------------------------------
ID1  15555555555    None  None
ID2         None  3Email  None
ID3         3123  4Email   aaa

table 派生两个表如下所示:

>>> table1=table[["ID","Phone","Email"]]
>>> table1

ID        Phone   Email
------------------------
ID1  15555555555    None
ID2         None  3Email
ID3         3123  4Email

>>> table2=table[["ID","Value"]]
>>> table2

ID Value
---------
ID1  None
ID2  None
ID3   aaa

现在我正在尝试使用 Golang 实现相同的目标。下面是我的第一步:

package main

import (
    "fmt"

)

type Identification struct {
    ID    string
    Phone int64
    Email string
    Value string
}

func main() {
    // define slice of Identification
    var idents []Identification
    idents = append(idents, Identification{ID: "ID1", Phone: 15555555555})
    idents = append(idents, Identification{ID: "ID2", Email: "3Email"})
    idents = append(idents, Identification{ID: "ID3",   Phone:3123, Email: "4Email", Value: "aaa"})

    fmt.Println(idents)

}

结果:

[{ID1 15555555555  } {ID2 0 3Email } {ID3 3123 3Email aaa}]

我的问题是如何像示例中的 Python 一样对 idents 进行切片?

【问题讨论】:

  • struct 具有固定的“形状”,因此您不能直接执行此操作。你为什么想要呢?您可以将字段保留在那里,并在处理时忽略它们。
  • 这似乎是一个 XY 问题。你的最终目标是什么?
  • @Flimzy 我的目标是从源中导入行数据,然后根据特定键对其进行切片并将其插入数据库中的不同表中
  • 那么没有必要在结构中“切片”它。只需将您关心的列插入相应的表中即可。

标签: dataframe go data-structures struct


【解决方案1】:

go 中的结构不能被拆分,你所能做的就是重置你想要去掉的字段的值。

您需要map 才能实现您在 Python 中看到的内容。但是 Go 是一种类型化语言,因此为了存储任意数据,您可以使用 interface{} 类型。因此,您需要使用map[string]interface{} 来存储不同类型的数据,否则将所有内容都设为字符串并使用map[string]string

那么对于切片,go 在标准包中没有任何东西可以切片列。幸运的是,有些人开发了开源软件包,让您的生活更轻松 :) 我建议您使用 https://github.com/go-gota/gota

如果你必须自己做,你可以这样做:

package main

import "fmt"

type table []map[string]interface{}

func (t table) sliceColumns(cols ...string) table {
        // create our new resulting table
        var newTable = make(table, len(t))

        // loop through table and populate our newTable
        for i, m := range t {

                var n = make(map[string]interface{}, len(cols))
                for _, col := range cols {
                        if v, ok := m[col]; ok {
                                n[col] = v
                        }
                }

                newTable[i] = n
        }

        return newTable
}

func main() {
        // define slice of Identification
        var t = table{
                {
                        "ID":    "1",
                        "Phone": 155555,
                },
                {
                        "ID":    "2",
                        "Email": "3Email",
                },
                {
                        "ID":    "3",
                        "Email": "4Email",
                        "Value": "aaaa",
                        "Phone": "123",
                },
        }

        fmt.Println(t.sliceColumns("ID", "Phone")) // [map[ID:1 Phone:155555] map[ID:2] map[ID:3 Phone:123]]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2018-06-15
    • 2023-03-14
    • 1970-01-01
    • 2023-02-16
    • 2019-03-06
    • 1970-01-01
    相关资源
    最近更新 更多