Println and printf are common methods in the FMT package, what is the difference between the two functions when you need to print the information?
Attached code
package mainimport ( "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) }}
Run results
%d is Man 1529049077 // println输出结果1529049077 is Man // printf输出结果
Results
Printf : Can print out a formatted string,
Println not;
Make a little change under
package mainimport "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)}
Results
Reason
See the source content, I believe you generally understand the difference between the two functions bar ~;
Bottom line: println will output according to your input format, printf needs to format output and output format;
Author: not moving Peak
Blog Park: http://www.cnblogs.com/mylly/
All rights reserved, welcome to keep the original link to reprint:)