go – 打印时不要在项目之间加空格

huangapple go评论67阅读模式
英文:

go - print without space between items

问题

我想要打印这两个字符串而不带空格填充,即"ab",但上面的代码会打印"a b"。

我应该改用Printf吗?

fmt.Printf("%s%s\n","a","b")

Go fmt

英文:
fmt.Println("a","b")

I want to print the two strings without space padding, namely "ab", but the above will print "a b".

Go fmt

Do I just switch to using Printf ?

fmt.Printf("%s%s\n","a","b")

答案1

得分: 18

普通的打印语句可以使用,只需将最后一个元素设置为"\n"。
如果你不习惯使用printf样式的格式化,使用print语句会更容易阅读。

在这里可以查看示例代码:

fmt.Println("a", "b")
fmt.Print("a", "b", "\n")
fmt.Printf("%s%s\n", "a", "b")

将会打印出:

a b
ab
ab

这里是示例代码的在线演示。

英文:

Plain old print will work if you make the last element "\n".
It will also be easier to read if you aren't used to printf style formatting.

See here on play

fmt.Println("a","b")
fmt.Print("a","b","\n")
fmt.Printf("%s%s\n","a","b")

will print:

a b
ab
ab

答案2

得分: 12

根据文档中的说明:

> Println使用其操作数的默认格式进行格式化,并写入标准输出。操作数之间始终添加空格,并附加换行符。它返回写入的字节数和遇到的任何写入错误。

因此,您可以按照您已经提到的方法操作,或者在打印之前连接字符串:

fmt.Println("a"+"b")

根据您的用例,您可以使用strings.Join(myStrings, "")来实现这个目的。

英文:

As it can be found in the doc:

> Println formats using the default formats for its operands and writes
> to standard output. Spaces are always added between operands and a
> newline is appended. It returns the number of bytes written and any
> write error encountered.

So you either need to do what you already said or you can concatenate the strings before printing:

fmt.Println("a"+"b")

Depending on your usecase you can use strings.Join(myStrings, "") for that purpose.

答案3

得分: 2

Println依赖于doPrint(args, true, true),其中第一个参数是addspace,第二个参数是addnewline。因此,Println带有多个参数时,将始终打印空格。

似乎没有调用doPrint(args, false, true),这是你想要的。Printf可能是一个解决方案,Print也可以,但你需要添加一个换行符。

英文:

Println relies on doPrint(args, true, true), where first argument is addspace and second is addnewline. So Prinln ith multiple arguments will always print space.

It seems there is no call of doPrint(args, false, true) which is what you want.
Printf may be a solution, Print also but you should add a newline.

答案4

得分: -1

你可以使用以下代码来替代Printf,但是需要进行基准测试以比较性能:

import "fmt"
import "strings"

fmt.Println(strings.Join([]string{"a", "b"}, ""))

记得导入strings包,并查看strings.Join的文档以了解更多信息。

英文:

You'd have to benchmark to compare performance, but I'd rather use the following than a Printf:

fmt.Println(strings.Join([]string{"a", "b"}, ""))

Remember to import "strings", and see strings.Join documentation for an explanation.

答案5

得分: -1

在我的项目中的解决方案

package main

import "fmt"

var formatMap = map[int]string{
    0: "",
    1: "%v",
}

func Println(v ...interface{}) {
    l := len(v)
    if s, isOk := formatMap[l]; !isOk {
        for i := 0; i < len(v); i++ {
            s += "%v"
        }
        formatMap[l] = s
    }
    s := formatMap[l] + "\n"
    fmt.Printf(s, v...)
}

func main() {
    Println()
    Println("a", "b")
    Println("a", "b")
    Println("a", "b", "c", 1)
}

这是你的项目中的代码部分。

英文:

the solution in my project

package main

import &quot;fmt&quot;

var formatMap = map[int]string{
    0: &quot;&quot;,
    1: &quot;%v&quot;,
}

func Println(v ...interface{}) {
    l := len(v)
    if s, isOk := formatMap[l]; !isOk {
        for i := 0; i &lt; len(v); i++ {
            s += &quot;%v&quot;
        }
        formatMap[l] = s
    }
    s := formatMap[l] + &quot;\n&quot;
    fmt.Printf(s, v...)
}
func main() {
    Println()
    Println(&quot;a&quot;, &quot;b&quot;)
    Println(&quot;a&quot;, &quot;b&quot;)
    Println(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, 1)
}

huangapple
  • 本文由 发表于 2014年9月19日 15:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/25928991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定