英文:
Get output in a specific format
问题
我写了一段 Go 代码,用于过滤包含 id、name 和 age 的数据。只有当数据中的年龄小于 21 岁时,才会将其打印到终端上。我使用了结构体切片和 for 循环来获取结果。以下是代码:
type data struct {
id int
name string
age int
}
func FilterData() {
var people = []data{
{id: 1, name: "Udin", age: 12},
{id: 2, name: "Wati", age: 51},
{id: 3, name: "Budi", age: 34},
{id: 4, name: "Agus", age: 16},
{id: 5, name: "Sari", age: 19},
{id: 6, name: "Ririn", age: 21},
}
var result = []data{}
for _, item := range people {
if item.age < 21 {
result = append(result, item)
}
}
fmt.Printf("%+v\n", result)
}
func main() {
FilterData()
}
它的输出结果如下:
[{id:1 name:Udin age:12} {id:4 name:Agus age:16} {id:5 name:Sari age:19}]
我需要以以下格式输出:
[
{ id: 1, name: 'Udin', age: 12 },
{ id: 4, name: 'Agus', age: 16 },
{ id: 5, name: 'Sari', age: 19 },
]
有没有办法得到所需的输出格式?
英文:
I write a go code that filters data containing id, name, and age. It is printed to the terminal only if the age in the data is under 21. I'm used slice of struct and for loop to get the result. Here is the code:
type data struct {
id int
name string
age int
}
func FilterData() {
var people = []data{
{id: 1, name: "Udin", age: 12},
{id: 2, name: "Wati", age: 51},
{id: 3, name: "Budi", age: 34},
{id: 4, name: "Agus", age: 16},
{id: 5, name: "Sari", age: 19},
{id: 6, name: "Ririn", age: 21},
}
var result = []data{}
for _, item := range people {
if item.age < 21 {
result = append(result, item)
}
}
fmt.Printf("%+v\n", result)
}
func main() {
FilterData()
}
It gives output like this:
[{id:1 name:Udin age:12} {id:4 name:Agus age:16} {id:5 name:Sari age:19}]
I need to get an output in the format:
[
{ id: 1, name: 'Udin', age: 12 },
{ id: 4, name: 'Agus', age: 16 },
{ id: 5, name: 'Sari', age: 19 },
]
Is there a way to get the desired format output?
答案1
得分: 1
这是基本实现。
package main
import (
"fmt"
)
type data struct {
id int
name string
age int
}
func print_data_array(datum []data) {
fmt.Println("[")
for _, data := range datum {
fmt.Printf(" %v,\n", format_data_struct(data))
}
fmt.Println("]")
}
func format_data_struct(d data) string {
// 注意:这里没有转义 d.name 中的引号。
return fmt.Sprintf(
`{ %v: %v, %v: "%v", %v: %v }`,
"id", d.id,
"name", d.name,
"age", d.age,
)
}
func main() {
var people = []data{
{id: 1, name: "Udin", age: 12},
{id: 2, name: "Wati", age: 51},
{id: 3, name: "Budi", age: 34},
{id: 4, name: "Agus", age: 16},
{id: 5, name: "Sari", age: 19},
{id: 6, name: "Ririn", age: 21},
}
print_data_array(people)
}
希望对你有帮助!
英文:
Here is basic implementation.
package main
import(
"fmt"
)
type data struct {
id int
name string
age int
}
func print_data_array(datum []data) {
fmt.Println("[")
for _, data := range(datum) {
fmt.Printf(" %v,\n", format_data_struct(data))
}
fmt.Println("]")
}
func format_data_struct(d data) (string) {
// Note: this does not escape quotes in d.name.
return fmt.Sprintf(
`{ %v: %v, %v: "%v", %v: %v }`,
"id", d.id,
"name", d.name,
"age", d.age,
)
}
func main() {
var people = []data{
{id: 1, name: "Udin", age: 12},
{id: 2, name: "Wati", age: 51},
{id: 3, name: "Budi", age: 34},
{id: 4, name: "Agus", age: 16},
{id: 5, name: "Sari", age: 19},
{id: 6, name: "Ririn", age: 21},
}
print_data_array(people)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论