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不行;
总结:
println会根据你输入格式原样输出,printf需要格式化输出并带输出格式;