duanhuoyao7011 2016-02-19 02:45
浏览 975
已采纳

在struct golang中打印一个struct

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条回答 默认 最新

  • dsoxcj7276 2016-02-19 03:03
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里