Whats idomatic Go for a Struct const?

xuanbao · · 615 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m porting a Java library to Go for work and we have a few<code>static final</code> variables in one of the classes. I know you can&#39;t do <code>const</code>structs in Go but I&#39;m at a loss how to do this.</p> <p>Here is the java line:</p> <pre><code>public static final MyClass AWESOME = new MyClass(&#34;Awesome&#34;); </code></pre> <p>I thought of creating a function like:</p> <pre><code>func Awesome() *MyClass { return &amp;MyClass{&#34;Awesome&#34;} } </code></pre> <p>But that doesn&#39;t stop users from changing what the pointer is point to. I would like to use a pointer just so I only need to create on instance of the struct but maybe it&#39;s not possible.</p> <p>Any ideas how to mimic this in Go?</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>Your <code>Awesome</code> function would create an instance of MyClass per call anyhow.</p> <p>I&#39;ve settled on a top-level &#34;var&#34; declaration with documentation that says &#34;Don&#39;t change this&#34;. There&#39;s only so much you can do.</p></pre>Destructicorn: <pre><p>Yeah, I did some Googling around after I posted this and exactly what you said was the closest I saw.</p></pre>mcandre: <pre><p>The public/private casing rule in Go can be leveraged to construct factory patterns: Mark the struct as private, and offer a public method that generates instances</p></pre>jerf: <pre><p>That&#39;s mostly orthogonal to this question. If you do</p> <pre><code>package sample var SupposedToBeConst = factoryFunction() </code></pre> <p>then other code can still do <code>sample.SupposedToBeConst = SomethingElse</code> if the type is public, which is a reasonable possibility. Making that type private has its own problems, because while Go does permit you to expose values that have unexported types, they are difficult to use. And the decision about whether to export the type or not is not one that you want to be making based on whether or not you&#39;ve got an exported &#34;constant&#34; you want to stay constant.</p> <p>So, in conclusion, I&#39;ve settled on a top-level &#34;var&#34; declaration with documentation that says &#34;Don&#39;t change this&#34;. I actually fiddled with quite a lot of possibilities before that, but they all come with pretty nasty side effects vs. just trusting people not to touch the variables.</p> <p>Also, people are generally <em>really</em> incentivized to not touch those variables because if you don&#39;t provide a sync.Mutex or something to synchronize those changes, they&#39;re unsafe to change anyhow because it&#39;s automatically a race condition once the the program gets past initialization. Granted, that&#39;s not the strongest protection in the world, but it&#39;s definitely a good reason to leave those variables alone unless something is explicitly documented about why it&#39;s OK to change them.</p></pre>Killing_Spark: <pre><p>Also, i mean if they change it and it breaks its kinda their own fault. You warned them not to do it. </p></pre>SYN_SYNACK_ACK: <pre><p>To prevent someone from creating multiple instances I recently started using this pattern</p> <pre><code>import ( &#34;sync&#34; ) var once sync.Once var awesome *MyClass type MyClass struct { string } func Awesome() *MyClass { once.Do(func() { awesome = &amp;MyClass{&#34;Awesome&#34;} }) return awesome } </code></pre> <p><a href="https://play.golang.org/p/yNoKLjoUXv" rel="nofollow">https://play.golang.org/p/yNoKLjoUXv</a></p></pre>tv64738: <pre><p>That&#39;s a lot of code for what amounts to</p> <pre><code>var Awesome = &amp;MyClass{&#34;Awesome&#34;} </code></pre></pre>Destructicorn: <pre><p>I&#39;ve recently been using that pattern also for Singletons in Go. I could use that but feels like it would be a lot of boilerplate to get what I&#39;m after, but that doesn&#39;t mean it&#39;s wrong. A lot of good ideas on this thread trying to figure out which one works best for our situation.</p></pre>earthboundkid: <pre><p>If you just port a library from Java to Go and keep the API the same, it won&#39;t be Go. You can do a one-to-one as a first step, but really, you should rethink the API as part of the complete move.</p></pre>Destructicorn: <pre><p>We&#39;re keeping the Java library along side the new Go one. I&#39;ve already redesigned some stuff maybe this requires a closer look.</p></pre>dinkumator: <pre><p>Plenty of examples in the stdlib. For example, the DefaultClient and DefaultServeMux in net/http: <a href="https://golang.org/pkg/net/http/#pkg-variables" rel="nofollow">https://golang.org/pkg/net/http/#pkg-variables</a></p> <p>Generally, if it&#39;s an interface and the implementation details should stay hidden, leave the type unexported, otherwise a default+accessible export type are better.</p></pre>SportingSnow21: <pre><p>Don&#39;t use an exported type. The compiler won&#39;t allow users to change it without hacking the library.</p></pre>epiris: <pre><p>Can&#39;t help if you say you want a static final java object. Instead describe the object itself, what a user can do to it. Then you can get the most idiomatic suggestion. What others posted here is reasonable, but you can in fact get the identical behavior as this. Just because you return a value doesn&#39;t mean you can&#39;t grant access to mutate or read the state that you want. Go strings and slices are a good example that I imagine will click right away what I&#39;m heading towards.</p> <pre><code>type state struct { privateFields string; ... } type MyClass struct { s *state } // now my class has methods to mutate / read state but // pass by Value only so no one can change what Awesome // returns. It&#39;s cheap as passing a pointer around so no real // penalties. var awesome = &amp;state{} func Awesome() { return MyClass{awesome} } // If you wanted to really go full anti pattern and // write something I could only describe as Gova // you could... type MyClass string const ( Awesome MyClass = &#34;Awesome&#34; ) func (s MyClass) SuchFinal(muchPrivate string) { govaMap[s].privateStuff = muchPrivate } var govaMap = map[MyClass]*state{ Awesome: &amp;state{...}, } </code></pre> <p>The reflect pkg Value T is a good example too, it always returns a value with three words that point to internal types. It still has a full (one of the largest in the std library in fact) API that mutates internal state. Still though, in the spirit of Go try to justify complexity, ask yourself: would i assign to a global variable &#34;by mistake&#34;. Other developers will have the same answer.</p></pre>paul2048: <pre><p>There are no final variables in Go. So there is no point trying to mimic final variables.</p></pre>egonelbre: <pre><p>There&#39;s an important piece missing. Why do you want to do all of this in the first place?</p></pre>Destructicorn: <pre><p>There overall goal is to port the Java library to Go in such a way that using it feels almost identical to the Java library. Implementation on the back end will be different to play more to Go&#39;s strengths. For this particular problem I wasn&#39;t sure in Go how to get the same feel for using that particular class as you would in Java. </p> <p>I&#39;ve resolved to starting to break down the Java convention and just do what feels right for Go.</p></pre>tv64738: <pre><blockquote> <p>port the Java library to Go in such a way that using it feels almost identical to the Java library.</p> </blockquote> <p>That&#39;s a bad idea. And replace Java and Go with any two programming languages and it&#39;s still a bad idea.</p></pre>

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

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