Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Println vs Printf vs Print in Go

Tags:

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

  1. Println
  2. Printf
  3. Print

So, If someone call tell the difference between three of them?

like image 416
anny123 Avatar asked Dec 21 '18 04:12

anny123


People also ask

What is the difference between Println and printf in go?

Here Printf formats according to a specified format specifier but Println uses the default formats for its operands. How to Install Go on Windows?

What is the difference between print Println and printf?

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.

What is purpose of Println in go?

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.

How does printf work in Golang?

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.


1 Answers

  • Printf - "Print Formatter" this function allows you to format numbers, variables and strings into the first string parameter you give it
  • Print - "Print" This cannot format anything, it simply takes a string and print it
  • Println - "Print Line" same thing as Printf() however it will append a newline character\n
like image 143
Nate Schreiner Avatar answered Sep 23 '22 02:09

Nate Schreiner