How to retrieve array of elements from array of structure in golang?

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

How to retrieve array of elements from array of structure in golang?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习golang,在这方面遇到了困难。我有一个结构体数组:

Users []struct {
   UserName string 
   Category string
   Age string
}

我想从这个结构体数组中提取所有的UserName。所以,输出的类型应该是:

UserList []string 

我知道可以使用循环的暴力方法手动提取元素,并从中构建一个数组。是否有其他方法可以做到这一点?

英文:

I am new to golang, and got stuck at this. I have an array of structure:

Users []struct {
   UserName string 
   Category string
   Age string
}

I want to retrieve all the UserName from this array of structure. So, output would be of type:

UserList []string 

I know the brute force method of using a loop to retrieve the elements manually and constructing an array from that. Is there any other way to do this?

答案1

得分: 30

不,循环是解决问题的方法。

这是一个可工作的示例。

package main

import "fmt"

type User struct {
	UserName string
	Category string
	Age      int
}

type Users []User

func (u Users) NameList() []string {
	var list []string
	for _, user := range u {
		list = append(list, user.UserName)
	}
	return list
}

func main() {
	users := Users{
		User{UserName: "Bryan", Category: "Human", Age: 33},
		User{UserName: "Jane", Category: "Rocker", Age: 25},
		User{UserName: "Nancy", Category: "Mother", Age: 40},
		User{UserName: "Chris", Category: "Dude", Age: 19},
		User{UserName: "Martha", Category: "Cook", Age: 52},
	}

	UserList := users.NameList()

	fmt.Println(UserList)
}
英文:

Nope, loops are the way to go.

Here's a working example.

package main

import "fmt"

type User struct {
	UserName string
	Category string
	Age      int
}

type Users []User

func (u Users) NameList() []string {
	var list []string
	for _, user := range u {
		list = append(list, user.UserName)
	}
	return list
}

func main() {
	users := Users{
		User{UserName: "Bryan", Category: "Human", Age: 33},
		User{UserName: "Jane", Category: "Rocker", Age: 25},
		User{UserName: "Nancy", Category: "Mother", Age: 40},
		User{UserName: "Chris", Category: "Dude", Age: 19},
		User{UserName: "Martha", Category: "Cook", Age: 52},
	}

	UserList := users.NameList()

	fmt.Println(UserList)
}

答案2

得分: 6

不,Go语言没有像Python或Ruby那样提供很多辅助方法。因此,你需要遍历结构体数组并填充你的数组。

英文:

No, go does not provide a lot of helper methods as python or ruby. So you have to iterate over the array of structures and populate your array.

答案3

得分: 4

不,没有现成的解决方案。

但是,有一个名为go-linqGo包,其中包含许多辅助方法可以实现这个功能。
https://github.com/ahmetb/go-linq

如果你导入这个包,你可以使用以下代码:

From(users).SelectT(func(u User) string { return u.UserName })

这个包是基于C# .NET的LINQ,非常适合进行这种类型的操作。

英文:

No, not out of the box.

But, there is a Go package which has a lot of helper methods for this.
https://github.com/ahmetb/go-linq

If you import this you could use:

From(users).SelectT(func(u User) string { return u.UserName })

This package is based on C# .NET LINQ, which is perfect for this kind of operations.

答案4

得分: 0

是的,你可以使用https://github.com/szmcdull/glinq来实现:

package main

import (
	"fmt"

	"github.com/szmcdull/glinq/garray"
)

func main() {
	var users []struct {
		UserName string
		Category string
		Age      string
	}
	var userNames []string
	userNames = garray.MapI(users, func(i int) string { return users[i].UserName })
	fmt.Printf("%v\r\n", userNames)
}
英文:

Yes you can. Using https://github.com/szmcdull/glinq you can do:

package main

import (
	"fmt"

	"github.com/szmcdull/glinq/garray"
)

func main() {
	var users []struct {
		UserName string
		Category string
		Age      string
	}
	var userNames []string
	userNames = garray.MapI(users, func(i int) string { return users[i].UserName })
	fmt.Printf("%v\r\n", userNames)
}

huangapple
  • 本文由 发表于 2015年12月9日 14:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/34172001.html
匿名

发表评论

匿名网友

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

确定