dtuy84033 2017-11-05 00:44
浏览 29
已采纳

当我只期望换行并且似乎没有指定前导空格时,为什么会有前导空格

The link: https://play.golang.org/p/1b5MbgIP2N

The code:

package main

import "fmt"

func main() {
    println("his power level is over 9000!!! KAKAROTO")
    println(test(2))
}

func test(x int) int {
    fmt.Println(x, "
", "new line here")
    fmt.Println("another line here")
    return x + 1
}

for some reason unknown to me, there appears to be one single leading whitespace character in front of "new" when the function is run. This might be something really obvious but what is happening? I don't see that I'm explicitly adding a space anywhere

  • 写回答

1条回答 默认 最新

  • dqingn8836 2017-11-05 01:03
    关注

    The documentation for fmt.Println says:

    Spaces are always added between operands and a newline is appended.

    There is a leading space because fmt.Println adds a space between the " " and "new line here" operands.

    The fmt.Println function adds spaces between operands for convenience. It saves adding a number of " " operands in typical use of the function.

    Change the code to fmt.Println(x, " new line here") to avoid the unwanted space.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备