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.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大