Println 和Printf 都是fmt包中公共方法;在需要打印信息時常用的函數,那么二函數有什么區別呢?
附上代碼
package main
import ("time""fmt")
const (
Man = 1
Female = 2
)
func main(){
timer := time.Now().Unix()
if(timer % Female == 0){
fmt.Println("%d is Female", timer)
fmt.Printf("%d is Female", timer)
}else{
fmt.Println("%d is Man", timer)
fmt.Printf("%d is Man", timer)
}
}
運行結果
%d is Man 1529049077 // println輸出結果
1529049077 is Man // printf輸出結果
結果可知
Printf: 可打印出格式化的字符串,Println不行;
稍做修改下
package main
import "fmt"
const (
StrN = "123"
IntN = 123
)
func main(){
fmt.Println(StrN)
fmt.Printf("%s\n",StrN)
fmt.Printf(StrN)
fmt.Println(IntN)
fmt.Printf("%d\n",IntN)
fmt.Printf(IntN)
}
結果
這里寫圖片描述
原因
Println函數
Printf函數
看到源碼內容,相信你大致明白兩函數的區別了吧~;
總結一句話: println會根據你輸入格式原樣輸出,printf需要格式化輸出并帶輸出格式;
作者:不動峰
博客園:http://www.cnblogs.com/mylly/
版權所有,歡迎保留原文鏈接進行轉載:)