从结果集中取出某一列的值组装成新的数组

从结果集中取出某一列的值组装成新的数组

package main

import (
	"fmt"
)

func main() {

	data := []map[string]string{
		{"name":"z","age":"18","sex":"nan"},
		{"name":"l","age":"19","sex":"nv"},
		{"name":"x","age":"20","sex":"nan"},
		{"name":"g","age":"18","sex":"nv"},
	}
	fieldname := "age"
	fmt.Println(sliceColumn(data,fieldname))
	// 输出结果:[18 19 20 18]
}

func sliceColumn(data []map[string]string,fieldname string) (result []string) {
	for _,v := range data{
		result = append(result,v[fieldname])
	}
	return result
}

你可能感兴趣的:(goland,golang)