Variables in a string when using backquotes?

polaris · · 584 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><strong>UPDATE: Got all the help I need! But feel free to keep the discussion going if there are any cool ideas out there :)</strong></p> <p>Since in go you can create a string like this:</p> <pre><code>stringExample := `here&#39;s the first line and the second and third fourth fifth` </code></pre> <p>I&#39;m wondering if there is any way to insert other string variables into this. Say we have variables x, y, z that are each strings of arbitrary size and content. Can I do something like:</p> <pre><code>stringExample := `x y z` </code></pre> <p>If something like this can&#39;t be done, any ideas that make &#34;concatenating&#34; strings of arbitrary length/content in the spirit of my line of thinking here would be really helpful to me :)</p> <p>Thanks in advance!</p> <p><strong>UPDATE: I finally settled on this format to accomplish my needs. It allows me to represent each line of the string as a separate line in code which helps with readability for my purposes. Thanks to</strong> <a href="/u/barsonme">u/barsonme</a> <strong>for the inspiration</strong></p> <pre><code>stringExample := x + &#34;\n&#34; + y + &#34;\n&#34; + z </code></pre> <hr/>**评论:**<br/><br/>barsonme: <pre><p>If the variables are strings you have several options. </p> <p>Number one (concatenation):</p> <pre><code>foo := &#34;world&#34; bar := &#34;hello &#34; + foo + &#34;!&#34; </code></pre> <p>Number two (fmt):</p> <pre><code>foo := &#34;world&#34; bar := fmt.Sprintf(&#34;hello, %s&#34;, foo) </code></pre> <p>Other options include writing into a bytes.Buffer, but I don&#39;t think that&#39;s exactly what you want.</p> <p>I&#39;d recommend reading <a href="https://github.com/golang/go/wiki/Learn">https://github.com/golang/go/wiki/Learn</a> it&#39;ll definitely help answer questions like this.</p></pre>Rabiesalad: <pre><p>That&#39;s really useful. I&#39;m looking at some of the documentation and it&#39;s unclear exactly what &#34;%s&#34; does in your example; would both your examples lead to the bar variable holding the same string?</p> <p>I think the first example you provided will probably do what I need, but it&#39;s not as pretty as it would be if it could be kept in a multiline format... but I guess I could do:</p> <pre><code>stringExample := x + &#34;\n&#34; + y + &#34;\n&#34; + z </code></pre> <p>I guess that sorta accomplishes the same &#34;style&#34; as using the backquotes though it looks way uglier. Which is not a problem at all for me, I think it accomplishes what I want. I just need to make sure I append + &#34;\n&#34; + to the end of each line to ensure there&#39;s a line break.</p> <p>Thoughts?</p></pre>ChristophBerger: <pre><p>stringExample := fmt.Sprintf(&#34;%s\n%s\n%n&#34;, x, y, z)</p> <p>%s is a placeholder for a string variable. Each occurrence in the format string gets replaced by one of the subsequent parameters.</p> <p>Likewise, %d turns an integer variable into a decimal string, %f turns a floating-point variable into its decimal-point representation, <a href="https://godoc.org/fmt" rel="nofollow">and so on.</a></p></pre>Rabiesalad: <pre><p>that clears it up, thanks!!!</p></pre>Fwippy: <pre><p>If you <em>just</em> want to put things on multiple lines, you can use <code>strings.Join([]string{x,y,z}, &#34;\n&#34;)</code> - note that this only puts newlines in between the strings, and not one at the end.</p></pre>Rabiesalad: <pre><p>Nice and simple, I&#39;ll definitely use this for <em>something</em> but for my current purpose being able to represent the separate lines by literally writing the code on separate lines will really help me with formatting and readability.</p> <p>Thanks for your help :)</p></pre>Fwippy: <pre><p>You could use text/template - probably overkill for the example you have here, but if you&#39;re formatting some longer multi-line document and just want to put in a few variables here and there, that&#39;s the way to do it.</p></pre>Rabiesalad: <pre><p>yeah I think that&#39;s probably overkill. I will keep that in mind in future though it may come in handy. Thanks!</p></pre>TheMerovius: <pre><p>Just as an aside: fmt.Sprintf is probably faster. You could do something like</p> <pre><code>fmt.Sprintf(`%s %s %s`, x, y, z) </code></pre> <p>if you want to, or just</p> <pre><code>fmt.Sprintf(&#34;%s\n%s\n%s&#34;, x,y,z) </code></pre></pre>TheMerovius: <pre><p>Okay, I was apparently wrong. Good to know. :)</p></pre>Rabiesalad: <pre><p>Thanks, this is also quite similar to what I&#39;m looking for; I&#39;ll have to see which runs faster in my use-case.</p> <p>Thanks!</p></pre>

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

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