尝试在Go中显示我的变量的值。

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

Trying to display the value of my variable in go

问题

我正在学习Go语言,但是我不明白为什么%v不起作用。

首先,我的Go版本是1.17.6。

然后,这是我的代码:

package main

import "fmt"

func main() {
        x := 16
        fmt.Println("%v", x)
}

这是输出结果:

%v 16

谢谢。

英文:

I'm learning go today and I can't understand why %v don't work.

First of all, my version of go is 1.17.6.

Then, here is my code:

package main

import "fmt"

func main() {
        x := 16
        fmt.Println("%v", x)
}

Here the output:

%v 16

Thanks

答案1

得分: 2

尝试这样写:

fmt.Printf("%v", x)

如果你想在打印后添加一行,可以使用:

fmt.Printf("%v\n", x)
英文:

try this:

fmt.Printf("%v", x)

To also add a line after printing this, you can use:

fmt.Printf("%v\n", x)

huangapple
  • 本文由 发表于 2022年1月10日 19:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/70651589.html
匿名

发表评论

匿名网友

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

确定