How do you map structs in Golang

agolangf · · 419 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Coming from C# .NET MVC background, I appreciate the utility of AutoMapper that maps my domain model with DTO. This significantly relieves the burden of mapping objects.</p> <p>Currently, I am working on my personal project that employs layered design. I am using DTO for client-side data representation. To be honest, my DTO is not much different from my business logic model, except certain data are removed in DTO for security reasons. I have a long list of domain objects, and each of them can be mapped to a variety of DTOs depending on the use case, and writing these mapper functions is tedious.</p> <p>So, do any of you know if there is any sort of mappers that can (partially) reduce the workload of writing mapper functions? Thanks! </p> <hr/>**评论:**<br/><br/>epiris: <pre><p>Oh my.. you want to serve bytes right? So do that. Add abstractions when doing that becomes tedious. Don&#39;t start software design at the abstractions. If you need help while serving your bytes than explain the <strong>problem</strong> here, not the solution you have chosen.. or in this case the entire abstract domain of the solution you have chosen. Questions in <a href="https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem" rel="nofollow">XY problem</a> form should be avoided in general, but in Go even more so because you will find the solutions are much different (simpler) than your current background.</p></pre>gac_web: <pre><p>There are no objects in Go, only values per spec. So define 2 structs, the &#34;object&#34; and the DTO.</p> <blockquote> <p>I have a long list of domain objects, and each of them can be mapped to a variety of DTOs depending on the use case, and writing these mapper functions is tedious.</p> </blockquote> <p>Write a generator ( a go program) that will look at Go ast and generate your mapper function from the struct definition ? </p></pre>FUZxxl: <pre><p>First struct maps, then commute.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

419 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传