以编程方式创建嵌套数组
I want to test my serialization format for robustnes and want to serialize a nested array. The Serialization actually dies at some point, but I don't know at what depth exactly. Right now I just define a large array manually, but I want to do it programatically, so I can figure out when exactly the depth is too high. Changing the code for each iteration is tedious...
l := [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]string {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{"a"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
What I want is something like this:
l := "a"
while(1){
l = {l} //nest the slice/array once more
print("round ", i)
i++
}
I'm a Golang newbie and I don't know how I can get around the type-safety in this case.
我想测试我的序列化格式的鲁棒性,并希望序列化嵌套数组。 序列化实际上在某个时候终止,但是我不知道确切的深度。 现在,我只是手动定义一个大数组,但是我想以编程方式进行操作,因此我可以弄清楚深度到底何时过高。 为每次迭代更改代码很繁琐... p>
l:= [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] string {{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{“一种”}}}}}}}}}}}}}}} }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
代码> PRE>
我想要的东西是这样的: p>
l:=“ a”
while(1){
l = {l} // //嵌套切片/数组 再一次
print(“ round”,i)
i ++
}
code> pre>
我是Golang新手,我不知道该怎么办 在这种情况下避免类型安全。 p>
div>
In go you can't change the type of a variable so you need to use interface{} as type var l interface{}
now you could do something like:
l = "a"
for i:= 0;i<3;i++{
l = []interface{}{l}
}
printing l would give the output [[[a]]]
, same as [][][]string{{{"a"}}}
.
A problem is that you lose your type and it will be impossible to do something like l[0][0][0]
to get the value. As far as I now you would need to cast everything l.([]interface{})[0].([]interface{})[0].([]interface{})[0].(string)