英文:
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")
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论