I come from a land of JS and have mostly used things like console.log
or console.error
Now, the tutorial I am following, the instructor over there did something like this
package main import "fmt" func main() { var FirstName = "Varun" var lastName = "bindal" fmt.Println(FirstName, lastName) fmt.Printf("%T", FirstName) }
Here he did PrintF to check type instead of Println. Initially, I thought that println prints in new Line so I changed my
fmt.Printf("%T", FirstName)
to
fmt.Println("%T", FirstName)
but this logged %T Varun
instead of telling me the type.
I went to their site to figure it out and was either unable to comprehend it or wasn't able to find it out.
Googling lead me know that there are three ways to log/print in Go
So, If someone call tell the difference between three of them?
Here Printf formats according to a specified format specifier but Println uses the default formats for its operands. How to Install Go on Windows?
The names are short versions of their function. println is short for "print line", meaning after the argument is printed then goes to the next line. printf is short for print formatter, it gives you the ability to mark where in the String variables will go and pass in those variables with it.
We use Println() to write the input data stream to the standard output. It is a variadic function, which means that it takes a variable number of input arguments. The functionality of Println() in Golang is similar to print in python or printf in C.
printf() function in Golang allows users to print formatted data. The function takes a template string that contains the text that needs to be formatted, plus some annotation verbs that tell the fmt functions how to format the trailing arguments.
Printf
- "Print Formatter" this function allows you to format numbers, variables and strings into the first string parameter you give itPrint
- "Print" This cannot format anything, it simply takes a string and print itPrintln
- "Print Line" same thing as Printf()
however it will append a newline character\n
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With