Golang默认情况下在字符串后面添加空格吗?

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

golang adding spaces after strings by default?

问题

我正在学习Go语言,其中一个让我感到烦恼的问题是,当我使用fmt.Println时,它会在每个参数之后添加一个空格(即使是变量)。有没有办法去掉这个空格,只有在引号中添加空格时才会添加一个空格呢?

英文:

I'm learning Go and one annoyance I have with it is when i use fmt.Println it adds a space after every argument passed (even variables). Is there a way to remove this space so it only adds a space if I add one in quotes?

答案1

得分: 3

使用格式化字符串和 "Printf" 函数。

fmt.Printf("字符串 %s, 整数 %d, 任意值 %v\n", "hello", 1, struct {}{})
英文:

Use "Printf" function with format string instead.

fmt.Printf("string %s, integer %d, anything %v\n", "hello", 1, struct {}{})

答案2

得分: 2

请尝试这样写:

func main() {
    fmt.Println("With", "Space")
    fmt.Printf("%s%s\n", "No", "Space")
}

示例

英文:

Try it like this

func main() {
    fmt.Println("With","Space")
    fmt.Printf("%s%s\n","No","Space")
}

example

答案3

得分: 2

如果你想要一个像println一样工作的东西,不需要放置%v标记,那么fmt.Print可以完成这个任务。

它不会在参数之间添加空格,也不会添加换行符。

例如:

fmt.Print("a","b","c","\n")

输出:
abc

英文:

If you want something that works like println, where you don't have to put %v markers, then fmt.Print does the job.

it doesn't add spaces between arguments, and also doesn't add a newline.

for example:

fmt.Print("a","b","c","\n")

prints:
abc

huangapple
  • 本文由 发表于 2015年8月15日 12:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/32021599.html
匿名

发表评论

匿名网友

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

确定