Question about Goa

xuanbao · · 658 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I&#39;m more or less following <a href="http://goa.design/learn/guide.html" rel="nofollow">this guide</a> but with my own API, just starting simple to learn the framework.</p> <p>I have one mediatype with the following type: ID (string) Name (string), and Href (string).</p> <p>I generated my code and went to modify the code of my resource to obtain a dummy example when I want to GET from an id: /myRessource/:id&#34;</p> <pre><code>// Show runs the show action. func (c *FactionController) Show(ctx *app.ShowFactionContext) error { // FactionController_Show: start_implement if ctx.FactionID == &#34;0&#34; { return ctx.NotFound() } faction := app.MyapiFaction{ ID: ctx.FactionID, Name: fmt.Sprintf(&#34;Faction %s&#34;, ctx.FactionID), Href: app.FactionHref(ctx.FactionID), } // FactionController_Show: end_implement return ctx.OK(&amp;faction) } </code></pre> <p>Problem is when I went to build my application, it complained on the name of ID, name, and Href that it cannot use type string as type *string. I tried to reference them, but it only worked for ID, name and href kept complaining about the same error.</p> <p>I went into the code and found that the generated code for my mediaType was using pointers for the 3 data fields. I removed that and now it compiles.</p> <p>The question remain, is it an error with the generator and I have to do that for every mediatype that it generates or am I making a mistake? </p> <p>I have another question regarding the bytes length shown on the terminal, what exactly is it counting? It doesn&#39;t seems to be counting the length of the bytes being sent to the client. I&#39;ve tested on multiple compression level of gzip and it stays the same.</p> <hr/>**评论:**<br/><br/>bketelsen: <pre><p>goa uses pointers because it makes it easy to determine if you&#39;re given an empty string vs a zero value. &#34;&#34; is different from an unitialized (and un provided) string, so a pointer will let you determine whether you were provided that field, or whether it came empty. To fix your problem, declare your strings before your struct. </p> <p>See this simple example:</p> <p><a href="https://play.golang.org/p/UynBuUKaEM" rel="nofollow">https://play.golang.org/p/UynBuUKaEM</a></p> <p>goa is doing the right thing here by protecting you from deserialization problems with Go and zero values. </p></pre>Kraigius: <pre><p>Thank you very much!</p></pre>raphaelsb: <pre><p>goa generates pointers because the attributes are not defined as required in the design so could be nil at runtime. If that&#39;s not the case and you know they will never be nil then change the design and add a Required statement, something like:</p> <pre><code>Attributes(func() { Attribute(&#34;id&#34;, UUID, &#34;Unique identifier&#34;) Attribute(&#34;href&#34;, String, &#34;User API href&#34;) Attribute(&#34;email&#34;, String, &#34;User email&#34;, func() { Format(&#34;email&#34;) }) Required(&#34;id&#34;, &#34;href&#34;, &#34;email&#34;) }) </code></pre> <p>This will cause the generated data structures to not use pointers for the corresponding fields.</p></pre>Kraigius: <pre><p>Thanks for the help!</p></pre>raphaelsb: <pre><p>Ah and the &#34;bytes&#34; value is the number of bytes encoded by the application encoder before any higher level processing (such as compression).</p></pre>Kraigius: <pre><p>Thank you.</p></pre>shovelpost: <pre><p>Is it really necessary to use a framework with special DSL to write microservices in Go?</p></pre>Kraigius: <pre><p>Absolutely not. The language have all the standard packages necessary to write microservices.</p> <p>I do know how the community feel about using frameworks and I appreciate the concerns to keep complexity and speed to a minimum.</p> <p>However, my time is limited. When I evaluated my options Goa came at the top because it&#39;s one of the only framework out there focused on developing a RESTFUL API.</p> <p>Let&#39;s be honest here, I don&#39;t know enough about HTTP and the related RFC to do all of this by myself.</p></pre>interactiv_: <pre><blockquote> <p>I do know how the community feel about using frameworks</p> </blockquote> <p>Only a vocal minority complains about other people using frameworks. Unfortunately this minority makes the whole Go community look really shitty, pedantic and close minded.</p> <p>The fact is the default server mux is useless on its own for anything serious. You can&#39;t write a maintainable application with it, you have to come up with a way to support url parameters , match handlers by methods ,a middle-ware stack, a way to inject dependencies into your handlers to make them testable and co ... And no ,the solutions involving a bunch of closures , or global variables like the gorilla-toolkit do not lead to maintainable code.</p> <p>I&#39;m pretty sure the OP questioning your choices don&#39;t even use that on its own. He is just doing some virtue signaling and think he can get away with that attitude because that&#39;s the community mindset. </p> <p>Ignore that, and think about your requirements, if a library fits your requirements then go for it.</p> <p>.you don&#39;t owe that person anything, his opinion doesn&#39;t matter.</p></pre>paddie: <pre><p>Also, GOA gives you automatic swagger specification for your API to document and auto-generate clients. Try doing that with the standard library, I dare you.</p></pre>interactiv_: <pre><p>Are you questioning his choice because you don&#39;t like Frameworks ? why do you care ? it&#39;s not like http.ServerMux is going to get anyone very far . Are you a go core library purist ? you never used a single library that you didn&#39;t write in all your life ? </p></pre>

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

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