golang相当于存储数据的DICT列表

golang相当于存储数据的DICT列表,go,Go,我在学咕噜。我试图存储一点数据,然后将其发送到文件 这个python块的golang等价物是什么?(口述清单) 使用地图切片或结构会更好吗 我希望能够过滤数据(例如,所有45岁以上的朋友)并对数据进行排序(假设我有12条以上的记录)。然后将其打印到屏幕上。在许多使用情况下,如果在python中使用dict,则需要在Go中使用struct以获得静态键入。在您的情况下,我觉得这就像是结构的一部分: type Friend struct{ Name string `json:"name"` A

我在学咕噜。我试图存储一点数据,然后将其发送到文件

这个python块的golang等价物是什么?(口述清单)

使用地图切片或结构会更好吗


我希望能够过滤数据(例如,所有45岁以上的朋友)并对数据进行排序(假设我有12条以上的记录)。然后将其打印到屏幕上。

在许多使用情况下,如果在python中使用dict,则需要在Go中使用struct以获得静态键入。在您的情况下,我觉得这就像是结构的一部分:

type Friend struct{
  Name string `json:"name"`
  Age  int    `json:"age"`
}

然后您可以序列化/反序列化到
[]*Person

Puuhon的
列表的等价物是一个
切片
,两者具有相同的语义和用例

但是在一片中放什么呢?这取决于你的口述。如果它们是相同的字段,我建议使用
struct
s。给它上面这样的字段。例如,有时必须存储不同的键和不同的字符串值。将其定义为字符串到字符串的映射:

map[string]string

作为最后手段,有可能制作动态类型的地图。但是不要过度使用它,因为你失去了静态输入的所有好处。程序变得更容易出错,速度也更慢

鉴于您的示例数据似乎是同质的(朋友共享他们的属性),您可以使用
struct
s的
slice
,如下所示:

type Friend struct {
    Name string
    Age  int
}

var friends []Friend = make([]Friend, 0)
现在,假设您已将朋友添加到该部分,您可以筛选年龄大于某个数字的朋友:

func filterFriendsAboveAge(allFriends []Friend, minAge int) []Friend {
    results := make([]Friend, 0) 
    for _, friend := range allFriends {
        if friend.Age > minAge {
            results = append(results, friend)
        }
    }
    return results
}

请注意,通过调用此函数,返回片中的友元值将是原始值的副本。如果需要保留标识,请改用指针。

下面是一个小示例程序,它可以满足您的需要:

package main

import (
    "fmt"
    "sort"
)

type friend struct {
    name string
    age  int
}

func main() {
    friends := []friend{
        {"James", 44},
        {"Jane", 47},
        {"Bob", 30},
        {"Cesar", 90},
        {"John", 45},
    }

    over45 := filterFriends(friends, func(f friend) bool {
        return f.age > 45
    })
    fmt.Println("over 45:", over45)

    // note that sort.Sort will change the contents of the slice; if you want
    // to keep the original order as well, you would first have to copy that
    // slice and sort the copy
    sort.Sort(byAge(friends))
    fmt.Println("sorted by age:", friends)
}

// filterFriends takes your slice and a predicate to filter by, then returns a
// newly allocated list of friends that made it through the filter.
func filterFriends(friends []friend, pred func(friend) bool) []friend {
    var fit []friend
    for _, f := range friends {
        if pred(f) {
            fit = append(fit, f)
        }
    }
    return fit
}

// byAge implements the sort.Interface so we can pass it to sort.Sort.
type byAge []friend

func (f byAge) Len() int           { return len(f) }
func (f byAge) Less(i, j int) bool { return f[i].age < f[j].age }
func (f byAge) Swap(i, j int)      { f[i], f[j] = f[j], f[i] }

直接比较将是一张地图,但如果没有实际问题,我们无法说哪个更好。
package main

import (
    "fmt"
    "sort"
)

type friend struct {
    name string
    age  int
}

func main() {
    friends := []friend{
        {"James", 44},
        {"Jane", 47},
        {"Bob", 30},
        {"Cesar", 90},
        {"John", 45},
    }

    over45 := filterFriends(friends, func(f friend) bool {
        return f.age > 45
    })
    fmt.Println("over 45:", over45)

    // note that sort.Sort will change the contents of the slice; if you want
    // to keep the original order as well, you would first have to copy that
    // slice and sort the copy
    sort.Sort(byAge(friends))
    fmt.Println("sorted by age:", friends)
}

// filterFriends takes your slice and a predicate to filter by, then returns a
// newly allocated list of friends that made it through the filter.
func filterFriends(friends []friend, pred func(friend) bool) []friend {
    var fit []friend
    for _, f := range friends {
        if pred(f) {
            fit = append(fit, f)
        }
    }
    return fit
}

// byAge implements the sort.Interface so we can pass it to sort.Sort.
type byAge []friend

func (f byAge) Len() int           { return len(f) }
func (f byAge) Less(i, j int) bool { return f[i].age < f[j].age }
func (f byAge) Swap(i, j int)      { f[i], f[j] = f[j], f[i] }
over 45: [{Jane 47} {Cesar 90}]
sorted by age: [{Bob 30} {James 44} {John 45} {Jane 47} {Cesar 90}]