go语言基础之Printf和Println的区别
1、示例
package main //必须有一个main包 import "fmt" func main() { a := 10 //一段一段处理,自动加换行 fmt.Println("a = ", a) //格式化输出, 把a的内容放在%d的位置 // "a = 10\n" 这个字符串输出到屏幕,"\n"代表换行符 fmt.Printf("a = %d\n", a) }
#执行结果:
a = 10 a = 10
示例2:
package main //必须有一个main包 import "fmt" func main() { a := 10 b := 20 c := 30 fmt.Println("a = ", a, ", b = ", b, ", c = ", c) fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c) }
#执行结果:
a = 10 , b = 20 , c = 30 a = 10, b = 20, c = 30
示例3:
package main //必须有一个main包 import "fmt" func main() { //'\'以反斜杠开头的字符是转义字符, '\n'代表换行 fmt.Printf("hello go%c", '\n') fmt.Printf("hello go\n") fmt.Printf("hello itcast") }
#执行结果:
hello go hello go hello itcas