Go语言中 Print,Println 和 Printf 的区别

Print 和 Println
这两个打印方式类似,只在格式上有区别

  1. Println 打印的每一项之间都会有空行,Print 没有,例如:
fmt.Println("go","python","php","javascript") // go python php javascript
fmt.Print("go","python","php","javascript") // gopythonphpjavascript
  1. Println 会自动换行,Print 不会,例如:
fmt.Println("hello")
fmt.Println("world")
// hello
// world
fmt.Print("hello")
fmt.Print("world")
// helloworld

Println 和 Printf

func main() {
    a:=10
    b:=20
    c:=30
    fmt.Println("a=", a , ",b=" , b , ",c=" , c)
    fmt.Printf("a=%d,b=%d,c=%d" , a , b , c)
}

%d 是占位符,表示数字的十进制表示。Printf 中的占位符与后面的数字变量一一对应。更多的占位符参考:点击此处

猜你喜欢

转载自www.cnblogs.com/wangcc7/p/12709607.html