<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's the first line
and the second
and third
fourth
fifth`
</code></pre>
<p>I'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't be done, any ideas that make "concatenating" 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 + "\n" +
y + "\n" +
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 := "world"
bar := "hello " + foo + "!"
</code></pre>
<p>Number two (fmt):</p>
<pre><code>foo := "world"
bar := fmt.Sprintf("hello, %s", foo)
</code></pre>
<p>Other options include writing into a bytes.Buffer, but I don't think that's exactly what you want.</p>
<p>I'd recommend reading <a href="https://github.com/golang/go/wiki/Learn">https://github.com/golang/go/wiki/Learn</a> it'll definitely help answer questions like this.</p></pre>Rabiesalad: <pre><p>That's really useful. I'm looking at some of the documentation and it's unclear exactly what "%s" 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'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 + "\n" +
y + "\n" +
z
</code></pre>
<p>I guess that sorta accomplishes the same "style" 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 + "\n" + to the end of each line to ensure there's a line break.</p>
<p>Thoughts?</p></pre>ChristophBerger: <pre><p>stringExample := fmt.Sprintf("%s\n%s\n%n", 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}, "\n")</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'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're formatting some longer multi-line document and just want to put in a few variables here and there, that's the way to do it.</p></pre>Rabiesalad: <pre><p>yeah I think that'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("%s\n%s\n%s", 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'm looking for; I'll have to see which runs faster in my use-case.</p>
<p>Thanks!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传