以特定格式获取输出

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

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: &quot;Udin&quot;, age: 12},
		{id: 2, name: &quot;Wati&quot;, age: 51},
		{id: 3, name: &quot;Budi&quot;, age: 34},
		{id: 4, name: &quot;Agus&quot;, age: 16},
		{id: 5, name: &quot;Sari&quot;, age: 19},
		{id: 6, name: &quot;Ririn&quot;, age: 21},
	}

	var result = []data{}

	for _, item := range people {
		if item.age &lt; 21 {
			result = append(result, item)
		}
	}
	fmt.Printf(&quot;%+v\n&quot;, 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: &#39;Udin&#39;, age: 12 },
  { id: 4, name: &#39;Agus&#39;, age: 16 },
  { id: 5, name: &#39;Sari&#39;, 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(
  &quot;fmt&quot;
)

type data struct {
    id   int
    name string
    age  int
}

func print_data_array(datum []data) {
  fmt.Println(&quot;[&quot;)
  for _, data := range(datum) {
    fmt.Printf(&quot;  %v,\n&quot;, format_data_struct(data))
  }
  fmt.Println(&quot;]&quot;)
}

func format_data_struct(d data) (string) {
  // Note: this does not escape quotes in d.name.
  return fmt.Sprintf(
    `{ %v: %v, %v: &quot;%v&quot;, %v: %v }`,
    &quot;id&quot;, d.id,
    &quot;name&quot;, d.name,
    &quot;age&quot;, d.age,
  )
}

func main() {
  var people = []data{
      {id: 1, name: &quot;Udin&quot;, age: 12},
      {id: 2, name: &quot;Wati&quot;, age: 51},
      {id: 3, name: &quot;Budi&quot;, age: 34},
      {id: 4, name: &quot;Agus&quot;, age: 16},
      {id: 5, name: &quot;Sari&quot;, age: 19},
      {id: 6, name: &quot;Ririn&quot;, age: 21},
  }

  print_data_array(people)
}

huangapple
  • 本文由 发表于 2022年12月23日 05:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/74893821.html
匿名

发表评论

匿名网友

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

确定