Go tool: convert []map[string]interface{} to []map of key, similar to PHP array_column

func ArrayColumn(input []map[string]interface{}, columnKey string) []interface{} {
	columns := make([]interface{}, 0.len(input))
	for _, val := range input {
		if v, ok := val[columnKey]; ok {
			if k, ok := v.(int64); ok {
				columns = append(columns, map[int64]map[string]interface{}{k: val})
			} else if s, ok := v.(string); ok {
				columns = append(columns, map[string]map[string]interface{}{s: val})
			} else {
				columns = append(columns, map[interface{}]map[string]interface{}{v: val})
			}
		}
	}
	return columns
}
Copy the code