Question about string and []string. Might require append()? Help me simplify this.

polaris · · 882 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a segment of code that is dealing with strings and string slices. I have an external go file that is used entirely for config purposes, and am attempting to pull in the elements of a string slice from it to the main source file. The code I want to simplify is:</p> <pre><code> args := []string{&#34;-dmS&#34;, screenname} args = append(args, invocation...) args = append(args, rootdir+servername+&#34;/&#34;+&#34;server.jar&#34;) </code></pre> <p>Where screenname, rootdir, and servername are all strings, and invocation is a []string. Ultimately, I want args to contain all the elements of invocation, but be sandwiched on both sides by my other data.</p> <p>How do I simplify this into something succinct and usable? This code as it is works, it&#39;s just ugly as all hell and I <em>know</em> that it&#39;s only this gross through my own inexperience.</p> <hr/>**评论:**<br/><br/>skarlso: <pre><p>You could try using <a href="https://golang.org/pkg/flag/" rel="nofollow">Flags</a> and then you could concatenate the variables in whatever order you want them. Unless you are not dealing with runtime arguments. </p> <p>Oh wait. You are using an external go file for confirmation? That&#39;s not right. You should use a json file for that! Are you restricted to a go file? If so, have the arguments in separate variables which you could then move around. </p> <p>Also, if you don&#39;t want to use append you could try using slice indexes, like args[1:] and copy, but essentially it would leaf to the same conclusion. Without restructuring the way you are handling input right now there are limited options at your disposal. </p></pre>nathanpaulyoung: <pre><blockquote> <p>You could try using Flags and then you could concatenate the variables in whatever order you want them. Unless you are not dealing with runtime arguments.</p> </blockquote> <p>That&#39;s certainly an idea, but I&#39;m not receiving very much info from the commandline at all. My command line invocation only looks something like &#34;msct start servername&#34;, where servername ends up being used in the invocation of a screen session for a Minecraft server. The actual invocation used for starting the server should be configurable, but not at runtime level.</p> <p>What&#39;s more, I&#39;m using codegangsta&#39;s CLI library, so I&#39;m not sure how I&#39;d fit flags into what I&#39;m already doing, or how it&#39;d help.</p> <blockquote> <p>Oh wait. You are using an external go file for confirmation? That&#39;s not right. You should use a json file for that! Are you restricted to a go file? If so, have the arguments in separate variables which you could then move around.</p> </blockquote> <p>I have every intention of eventually moving to toml or yaml. This is for my own personal use right now, and I just really don&#39;t mine a recompile to change a setting atm. Some day, it&#39;ll be on github, and config will matter.</p> <blockquote> <p>Also, if you don&#39;t want to use append you could try using slice indexes, like args[1:] and copy, but essentially it would leaf to the same conclusion. Without restructuring the way you are handling input right now there are limited options at your disposal.</p> </blockquote> <p>I don&#39;t think I understand.</p></pre>skarlso: <pre><p>Yeah, like I said, if you don&#39;t change the way you are parsing the setup, you have very limited options. :) </p> <p>Forget about the copy. :) It would have been just as ugly, if not uglier. :) I would have suggested to use inserts with specific place and copy slice. But with append you are at least not confined to anything.</p></pre>nathanpaulyoung: <pre><p>Yeah, since my OP and y&#39;all&#39;s replies, I&#39;ve been thinking a lot about what I&#39;m trying to do. I think the solution really is just to switch to a proper config file format. I&#39;m looking at <a href="https://github.com/olebedev/config" rel="nofollow">olebedev/config</a>, and it seems really nice. Have you used it?</p></pre>joushou: <pre><p>JSON is not a very good format for configuration purposes. It quickly becomes unpleasant. I would suggest YAML, TOML, CSON, or even just newline separated key-value pairs. As someone dealing with <em>huge</em> JSON configuration files, you&#39;ll thank me once you get more than 3 keys in your file.</p></pre>skarlso: <pre><p>Let&#39;s agree to disagree. Json is a breeze to parse. And all the xML stuff is ugly and too verbose. :-)</p></pre>joushou: <pre><p>This is gross due to &#34;invocation&#34; being a slice. Could you give &#34;screenname&#34; as an argument <em>after</em> your invocation? That would make the code:</p> <pre><code>args := append(invocation, &#34;-dmS&#34;, screenname, rootdir+servername+&#34;/server.jar&#34;) </code></pre> <p>You could also consider if you could use the result as a string:</p> <pre><code>args := fmt.Sprintf(&#34;-dmS %s %s %s%s/server.jar&#34;, screenname, strings.Join(invocation, &#34; &#34;), rootdir, servername) </code></pre> <p>As for using a go file for configuration, it depends on the purpose. You could have a go file with compile-time constants if these options are not meant to change for <em>any</em> reason after delivery. Server configuration, however, should be in an external file of some sort, although I would <em>really</em> suggest that you didn&#39;t use JSON.</p></pre>nathanpaulyoung: <pre><blockquote> <p>Could you give &#34;screenname&#34; as an argument after your invocation?</p> </blockquote> <p>I don&#39;t think I have that option. I&#39;m using this as a server management tool for a Minecraft server, and I&#39;m running it in a screen. I&#39;m using codegangsta&#39;s cli library and receiving the servername on c.Args().First(). Then I build out the full body of exec.Command(cmd, args...), where args ends up being something along the lines of &#34;-dmS msct-servername java -jar /full/path/to/servername/server.jar&#34;, and cmd obviously contains the actual &#34;screen&#34; command.</p> <p>So I&#39;m really just bumbling around trying to learn what defines &#34;good form&#34; for me. I did actually try fmt.Sprintf for the full invocation at one point, but exec.Command() didn&#39;t seem to like it.</p> <blockquote> <p>You could have a go file with compile-time constants if these options are not meant to change for any reason after delivery. Server configuration, however, should be in an external file of some sort, although I would really suggest that you didn&#39;t use JSON.</p> </blockquote> <p>Yeah, I may look into toml or yaml. Eventually this will be code up on github for people to use. Until then, recompiling to change options doesn&#39;t matter to me.</p></pre>

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

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