您如何将树结构处理为已编译的字符串?

I am trying to understand how to traverse a tree structure and generate a string from the shape.

Any information about how to break this problem down would be much appreciated, really struggling to comprehend it.

If you imagine the following tree:

[
  {
    name: '1',
    children: [
      {
        name: '2',
        children: []
      },
      {
        name: '3',
        children: [
          {
            name: '4',
            children: []
          }
        ]
      }
    ]
  }
]

each node has the type of

type node struct {
    name      string
    children  []Node
}

I need to assemble a string out of it where each node corresponds to this signature

fn(name, children)

So the processed result of the above tree should be a string which resembles:

`fn("1", [ fn("2", []), fn("3", [ fn("4", []) ]) ])`