Go Learning Path: The difference between Println and Printf

Source: Internet
Author: User

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:)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.