<p><a href="https://gist.github.com/arehmandev/87a63bf5964ce4a8833294fb831c6373">Link here!</a></p>
<p>Hi I've managed to do this in hacky ways (done in Groovy, some horrendous looking code) - what would be the best way of achieving this transformation?</p>
<p>Edit: Added a solution contributed by <a href="/u/titpetric">u/titpetric</a> - big thanks</p>
<hr/>**评论:**<br/><br/>mm_5687: <pre><p>For those like me who struggled to spot the difference: <a href="https://i.imgur.com/BaFPwPO.png" rel="nofollow">https://i.imgur.com/BaFPwPO.png</a></p></pre>titpetric: <pre><p>Is the order of the items important? If yes, you'll have to loop through the outer slice of <code>[]Container</code> and match individual container paths to merge everything together.</p>
<p>If the order isn't important, you can create a <code>map[string]Container</code> where you would use container.Path as the key, and append components to it when a key would exist.</p>
<p>If you want to do this efficiently, you'll have to avoid reallocations. A way to do this would be to loop once over the containers to produce a map[string]int (key=container.path, value=count(containers with same path)). You can use <code>make(map[string]int, len(containers))</code> to produce one allocation, which allocates to your worst case scenario.</p>
<p>Everything comes down to what you understand about <code>best</code>. If you want the fastest, then the best way to measure that would be to <a href="https://scene-si.org/2017/06/06/benchmarking-go-programs/" rel="nofollow">create some benchmarks</a>.</p>
<p>edit: p.s. write a test in <a href="https://play.golang.org" rel="nofollow">go playground</a> as it makes it easier for other people to improve it. I would gladly rewrite a functional example, but I don't want to write the whole thing.</p></pre>absdevops: <pre><p>Big thanks.. a map was what I was missing! - I've gotten <a href="https://play.golang.org/p/4ucN97nnvp" rel="nofollow">here so far!</a></p></pre>titpetric: <pre><p><a href="https://play.golang.org/p/B6GhzQ_JmE" rel="nofollow">https://play.golang.org/p/B6GhzQ_JmE</a> - I eliminated a bunch of excess code. The result is printed out. If you want a slice instead of a map at the end, you'll just need to range over it once more. You can create it efficiently with <code>s := make([]Containers, len(m))</code> and then append() each without additional allocations.</p>
<p>Edit: here's a slice result as well: <a href="https://play.golang.org/p/St6-BOxSY6" rel="nofollow">https://play.golang.org/p/St6-BOxSY6</a> as I'm in the mood :)</p></pre>absdevops: <pre><p>Ah, reading it now it makes so much sense, yet I was having a brain block looking at it earlier. I'm still learning so big thanks!</p></pre>diegobernardes: <pre><p>This is pretty easy with Go: <a href="https://golang.org/pkg/encoding/json/#example__customMarshalJSON" rel="nofollow">https://golang.org/pkg/encoding/json/#example__customMarshalJSON</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传