<\/span><\/h2>\n\n\n\nBelow is the function prototype of Println<\/strong><\/p>\n\n\n\nfunc Println(a ...interface{}) (n int, err error)<\/code><\/pre>\n\n\n\nPrintln<\/strong> takes a variable number of arguments where each argument is an empty interface. It returns the number of characters printed and any error if happens. Since the argument type is an empty interface we can pass any data type to it. We can pass a string, int, float, struct, or any other data type. Each of the arguments to the Println<\/strong> function is formatted according to the default format specifier of that argument type. For example, the struct will be formatted according to the below specifier<\/p>\n\n\n\n%v<\/code><\/pre>\n\n\n\nThis format specifier only prints the Value part in the struct. Let’s see an example<\/p>\n\n\n\n
package main\nimport \"fmt\"\ntype employee struct {\n Name string\n Age int\n}\nfunc main() {\n name := \"John\"\n age := 21\n fmt.Println(\"Name is: \", name)\n fmt.Println(\"Age is: \", age)\n e := employee{\n Name: name,\n Age: age,\n }\n fmt.Println(e)\n fmt.Println(\"a\", 12, \"b\", 12.0)\n\n \n bytesPrinted, err := fmt.Println(\"Name is: \", name)\n if err != nil {\n\tlog.Fatalln(\"Error occured\", err)\n }\n fmt.Println(bytesPrinted)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nName is: John\nAge is: 21\n{John 21}\na 12 b 12\nName is: John\n14<\/code><\/pre>\n\n\n\nSome important points to note about the Println<\/strong> function<\/p>\n\n\n\nIt appends new line at the end. That is why each of the output is on a different line<\/li><\/ul>\n\n\n\nEach of the arguments will be separated by space in the output. That is why<\/li><\/ul>\n\n\n\nfmt.Println(\"Name is: \", name)<\/code><\/pre>\n\n\n\nprints<\/p>\n\n\n\n
Name is: John<\/code><\/pre>\n\n\n\nSpace is introduced automatically between the two arguments.<\/p>\n\n\n\n
It returns the number of characters printed or any error if happens<\/li><\/ul>\n\n\n\nbytesPrinted, err := fmt.Println(\"Name is: \", name)\nif err != nil {\n log.Fatalln(\"Error occured\", err)\n}\nfmt.Println(bytesPrinted)<\/code><\/pre>\n\n\n\nwill output below<\/p>\n\n\n\n
Name is: John\n14<\/code><\/pre>\n\n\n\nNumber of bytesPrinted<\/strong> is 14 as 14 characters are outputted<\/p>\n\n\n\n<\/span>About Print function<\/strong><\/span><\/h2>\n\n\n\nFunction prototype of Print<\/strong><\/p>\n\n\n\nfunc Print(a ...interface{}) (n int, err error)<\/code><\/pre>\n\n\n\nThe Print<\/strong> function is exactly the same as the Println<\/strong> function other than two differences<\/p>\n\n\n\nIt does not append a new line at the end. We need to use the new line identifier to add a new line “\\n”.<\/li><\/ul>\n\n\n\nSpace is only added between the arguments if neither of the operands is a string<\/li><\/ul>\n\n\n\nLet’s see an example for the same<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\ntype employee struct {\n\tName string\n\tAge int\n}\n\nfunc main() {\n\tname := \"John\"\n\tage := 21\n\tfmt.Print(\"Name is:\", name, \"\\n\")\n\tfmt.Print(\"Age is:\", age, \"\\n\")\n\n\te := employee{\n\t\tName: name,\n\t\tAge: age,\n\t}\n\n\tfmt.Print(e, \"\\n\")\n\n\tfmt.Print(\"a\", 12, \"b\", 12.0, \"\\n\")\n\n\tfmt.Print(12, 12.0, \"\\n\")\n \n \n bytesPrinted, err := fmt.Print(\"Name is: \", name, \"\\n\")\n\tif err != nil {\n\t\tlog.Fatalln(\"Error occured\", err)\n\t}\n\tfmt.Print(bytesPrinted)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nName is:John\nAge is:21\n{John 21}\na12b12\n12 12\nName is: John\n14<\/code><\/pre>\n\n\n\nSome important points to note about the Print<\/strong> function<\/p>\n\n\n\nIt does not append a new line at the end. That is why need to use “\\n”<\/strong> for adding a new line.<\/li><\/ul>\n\n\n\nIt only adds space between two arguments if each of them is a non-string. That is why<\/li><\/ul>\n\n\n\nfmt.Print(12, 12.0, \"\\n\")<\/code><\/pre>\n\n\n\nprints<\/p>\n\n\n\n
12 12<\/code><\/pre>\n\n\n\nwhile<\/p>\n\n\n\n
fmt.Print(\"a\", 12, \"b\", 12.0, \"\\n\")<\/code><\/pre>\n\n\n\nprints<\/p>\n\n\n\n
a12b12<\/code><\/pre>\n\n\n\nIt also returns the number of characters printed and any error if happens<\/li><\/ul>\n\n\n\n<\/span>About Printf function<\/strong><\/span><\/h2>\n\n\n\nFunction prototype of Printf<\/strong><\/p>\n\n\n\nfunc Printf(format string, a ...interface{}) (n int, err error)<\/code><\/pre>\n\n\n\nPrintf<\/strong> is also a variadic function meaning that it can have multiple arguments. There are two important points about its argument list<\/p>\n\n\n\nNotice that the first argument is a format<\/strong> or template<\/strong> string.<\/li><\/ul>\n\n\n\nThe next is a variable number of arguments. Each of the argument in this list could be string, int, struct, or anything. Due to same reason as above that is why it is an empty interface<\/li><\/ul>\n\n\n\nThe format<\/strong> or template<\/strong> string contains the actual string that needs to be formatted plus some formating verbs. These formating verbs tell how the trailing arguments will be formatted in the final string. So basically the format string argument contains certain symbols which are replaced by trailing arguments. <\/p>\n\n\n\nEg<\/p>\n\n\n\n
<\/span>Printing a string variable<\/strong><\/span><\/h3>\n\n\n\n%s<\/strong> symbol is used<\/li><\/ul>\n\n\n\nExample <\/li><\/ul>\n\n\n\nname := \"John\"\nfmt.Printf(\"Name is: %s\\n\", name)<\/code><\/pre>\n\n\n\n<\/span>Printing an integer<\/strong><\/span><\/h3>\n\n\n\n%d<\/strong> symbol is used<\/li><\/ul>\n\n\n\nExample <\/li><\/ul>\n\n\n\nage := 21\nfmt.Printf(\"Age is: %d\\n\", age)<\/code><\/pre>\n\n\n\n<\/span>Printing a struct<\/strong><\/span><\/h3>\n\n\n\nFor example, there are three format specifiers for printing a struct. <\/p>\n\n\n\n
%v<\/strong> \u2013 It will print only values. The field name will not be printed. This is the default way of printing a struct when using Println<\/li><\/ul>\n\n\n\n%+v \u2013 <\/strong>It will print both field and value. <\/li><\/ul>\n\n\n\n%#v \u2013 <\/strong>It will print the struct, also both field name and value<\/li><\/ul>\n\n\n\nThat is why<\/p>\n\n\n\n
fmt.Printf(\"Employee is %v\\n\", e)\nfmt.Printf(\"Employee is %+v\\n\", e)\nfmt.Printf(\"Employee is %#v\\n\", e)<\/code><\/pre>\n\n\n\nprints below respectively<\/p>\n\n\n\n
Employee is {John 21}\nEmployee is {Name:John Age:21}\nEmployee is main.employee{Name:\"John\", Age:21}<\/code><\/pre>\n\n\n\nIt is as per the explanation above.<\/p>\n\n\n\n
Also, note that this function returns the number of characters printed and any error if happens. Unlike Println<\/strong> it does add a new line. You will have to add “\\n”<\/strong> explicitly. <\/p>\n\n\n\nHere is the working program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n)\n\ntype employee struct {\n\tName string\n\tAge int\n}\n\nfunc main() {\n\tname := \"John\"\n\tage := 21\n\n\tfmt.Printf(\"Name is: %s\\n\", name)\n\tfmt.Printf(\"Age is: %d\\n\", age)\n\n\tfmt.Printf(\"Name: %s Age: %d\\n\", name, age)\n\n\te := employee{\n\t\tName: name,\n\t\tAge: age,\n\t}\n\n\tfmt.Printf(\"Employee is %v\\n\", e)\n\tfmt.Printf(\"Employee is %+v\\n\", e)\n\tfmt.Printf(\"Employee is %#v\\n\", e)\n\n\tbytesPrinted, err := fmt.Printf(\"Name is: %s\\n\", name)\n\tif err != nil {\n\t\tlog.Fatalln(\"Error occured\", err)\n\t}\n\tfmt.Println(bytesPrinted)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nName is: John\nAge is: 21\nName: John Age: 21\nEmployee is {John 21}\nEmployee is {Name:John Age:21}\nEmployee is main.employee{Name:\"John\", Age:21}\nName is: John\n14<\/code><\/pre>\n\n\n\nNotice that in below Printf<\/strong><\/p>\n\n\n\nfmt.Printf(\"Name: %s Age: %d\\n\", name, age)<\/code><\/pre>\n\n\n\n%s <\/strong>is replaced by name.<\/li><\/ul>\n\n\n\n%d<\/strong> is replaced by age.<\/li><\/ul>\n\n\n\nSo basically the symbols or verbs in the format string argument are replaced by trailing arguments in order<\/strong>If the number of format specifiers in the format string does not match the number of next variable arguments then the format specifier will be printed as is. For example, in the below code, we have two format specifier<\/p>\n\n\n\n%d<\/strong><\/li><\/ul>\n\n\n\n%s<\/strong><\/li><\/ul>\n\n\n\nWhile the next variable number of arguments is only one. Hence when we print it then it will print the second format specifier as is with MISSING as warning<\/p>\n\n\n\n
package main\nimport \"fmt\"\ntype employee struct {\n Name string\n Age int\n}\nfunc main() {\n name := \"John\"\n fmt.Printf(\"Name is: %s %d\\n\", name)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nName is: John %!d(MISSING)<\/code><\/pre>\n\n\n\nAlso, check out our Golang advance tutorial Series – Golang Advance Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewAbout Println functionAbout Print functionAbout Printf functionPrinting a string variablePrinting an integerPrinting a struct Overview Println, Print, and Printf are defined in the fmt package and are used to…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-5496","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Println vs Print vs Printf in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n