英文:
print a struct within a struct golang
问题
我想打印一个嵌套在结构体中的特定项。例如:
假设我已经创建了一个蓝图结构体,并正在创建一个新的结构体。
假设我有一个myshapes结构体,其中包含一个标题、圆的数量和正方形的数量。
car := blueprints{
dots: 5,
lines: 25,
shapes: []myshapes{
myshapes{
title: "car #1",
circles: 5,
squares: 7,
},
myshapes{
title: "car #2",
circles: 2,
squares: 14,
},
}
}
如何打印:
title: "car #1"
circles: 5
squares: 7
title: "car #2"
circles: 2
squares: 14
英文:
I want to print a certain item within a struct, that is within a struct. Example:
Pretend I have made up a blueprint struct, and are making an new one
Pretend I have a myshapes struct, that contains a title, # of circles, and squares
car := blueprints{
dots: 5,
lines: 25,
shapes: []myshapes{
myshapes{
title:"car #1",
circles:5,
squares:7,
},
myshapes{
title:"car #2",
circles:2,
squares:14,
},
}
How do I print:
title:"car #1"
circles:5
squares:7
title:"car #2"
circles:2
squares:14
答案1
得分: 1
下面的示例展示了如何打印结构体的特定字段值:
type child struct {
name string
age int
}
type parent struct {
name string
age int
children child
}
func main() {
family := parent{
name: "John",
age: 40,
children: child{
name: "Johnny",
age: 10,
},
}
fmt.Println(family.children.name) // 打印 "Johnny"
}
上面的代码将打印 "Johnny",这是父结构体中的一个结构体的值。然而,这段代码并没有太多意义,因为字段是 children
,但实际上只能有一个孩子作为其值。
让我们利用切片。现在我们知道如何打印特定的值,我们只需要在切片上循环并以与上面相同的方式打印即可。
我们可以这样做:
type child struct {
name string
age int
}
type parent struct {
name string
age int
children []child
}
func main() {
family := parent{
name: "John",
age: 40,
children: []child{
child{
name: "Johnny",
age: 10,
},
child{
name: "Jenna",
age: 7,
},
},
}
for _, child := range family.children {
fmt.Println(child.name)
}
}
上面的示例将打印 "Johnny" 和 "Jenna"。
你可以在自己的代码中使用类似上面展示的模式,循环遍历结构体并打印出你想要的任何值
请记住,有很多遍历的方法。例如,以下所有循环都会从上面的示例中打印出 "Johnny" 和 "Jenna"。
for _, child := range family.children { // 打印 "Johnny" 和 "Jenna"
fmt.Println(child.name)
}
for i, _ := range family.children { // 打印 "Johnny" 和 "Jenna"
fmt.Println(family.children[i].name)
}
for i := 0; i < len(family.children); i++ { // 打印 "Johnny" 和 "Jenna"
fmt.Println(family.children[i].name)
}
英文:
The example below shows how you'd print a particular field value of a struct:
type child struct {
name string
age int
}
type parent struct {
name string
age int
children child
}
func main() {
family := parent{
name: "John",
age: 40,
children: child{
name: "Johnny",
age: 10,
},
}
fmt.Println(family.children.name); // Prints "Johnny"
}
The code above would print "Johnny", which is a value of a struct within the parent struct. The code doesn't make much sense, however, because the field is children
, yet there is only ever able to be one child as its value.
Let's leverage slices. Now that we know how to print a particular value, all we would need for your case is to loop over a slice and print the same way we did above.
We can do that like this:
type child struct {
name string
age int
}
type parent struct {
name string
age int
children []child
}
func main() {
family := parent{
name: "John",
age: 40,
children: []child{
child{
name: "Johnny",
age: 10,
},
child{
name: "Jenna",
age: 7,
},
},
}
for _, child := range family.children {
fmt.Println(child.name);
}
}
The above example will pront "Johnny" and "Jenna".
You can use a similar pattern to what I showed above in your own code to loop through your structs and print whatever value your heart desires
Keep in mind, there are many ways to loop through things. For example, all the following loops would print "Johnny" and "Jenna" from the examples above.
for _, child:= range family.children{ // Prints "Jonny" and "Jenna"
fmt.Println(child.name);
}
for i, _ := range family.children{ // Prints "Jonny" and "Jenna"
fmt.Println(family.children[i].name);
}
for i := 0; i < len(family.children); i++ { // Prints "Jonny" and "Jenna"
fmt.Println(family.children[i].name);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论