32

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?

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • 2
    [The `fmt` package is well-documented.](https://golang.org/pkg/fmt/) – Adrian Dec 21 '18 at 15:23
  • 12
    When the question states they have read the documentation and cannot understand it, stating the documentation is good and downvoting the question will not help. Upvoting as it seems the OP has made the effort to try and reasurch but failed. – TafT Feb 26 '19 at 10:12

4 Answers4

45
  • 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 Print() however it will append a newline character \n at the end.
Yeonho
  • 3,629
  • 4
  • 39
  • 61
Nate Schreiner
  • 759
  • 2
  • 7
  • 15
  • 4
    `Println` also inserts spaces between arguments. `Print` only inserts spaces between arguments when either argument is *not* a string. – Joel Cornett Dec 21 '18 at 05:08
  • 2
    Do you mean `Println` is the same as `Print()`? Only `Printf()` does substitute formatting, the other functions just print the given arguments after each other (with spaces inserted or not and newline or not) – Ward D.S. May 19 '20 at 11:03
  • No. Printf() and Println() both do substitute formatting, however Println() will append a newline character to the argument(s) where as Printf() will not – Nate Schreiner May 20 '20 at 19:01
  • `Println` is similar to `Print`, not `Printf`. The answer was edited incorrectly(maybe just a typo but important...). The original answer was fine. @NateSchreiner I think it's not. `Println` does not do formatting, neither does `Print`. Only `Printf` does, among those three. – starriet Feb 28 '22 at 07:48
  • @starriet edited the answer to fix the typo! – Yeonho Jun 05 '22 at 08:06
  • Also `Println` automatically add verb 'v' to format. You can see this in `fmt` source code. – Ireina Apr 22 '23 at 08:35
23

Just as Nate said: fmt.Print and fmt.Println print the raw string (fmt.Println appends a newline)

fmt.Printf will not print a new line, you will have to add that to the end yourself with \n.

The way fmt.Printf works is simple, you supply a string that contains certain symbols, and the other arguments replace those symbols. For example:

fmt.Printf("%s is cool", "Bob") 

In this case, %s represents a string. In your case, %T prints the type of a variable.

novalagung
  • 10,905
  • 4
  • 58
  • 82
robert
  • 731
  • 1
  • 5
  • 22
0

To answer your question,

fmt.Print with \n and " " is like fmt.Println.

fmt.Printf with \n and %v is like fmt.Println.

as shown in this example:

package main

import "fmt"

func main() {
    const name, age = "Kim", 22
    //
    fmt.Print(name, " is ", age, " years old.\n")  // Kim is 22 years old.
    fmt.Printf("%v is %v years old.\n", name, age) // Kim is 22 years old.
    fmt.Println(name, "is", age, "years old.")     // Kim is 22 years old.
    //
    print(name, " is ", age, " years old.\n") // Kim is 22 years old.
    println(name, "is", age, "years old.")    // Kim is 22 years old.
}

print and println are like fmt.Print and fmt.Println with qualification. See https://stackoverflow.com/a/48420811/12817546 and https://stackoverflow.com/a/14680544/12817546.

Go offers many other ways to format I/O. See https://golang.org/pkg/fmt.

-2

fmt.Println("Value of Pi :", math.Pi)

fmt.Printf("Value of Pi : %g", math.Pi)

expect //

Value of Pi : 3.141592653589793

Value of Pi : 3.141592653589793

Pierre B
  • 15
  • 3