Alwaysblue
Alwaysblue

Reputation: 11830

Println vs Printf vs Print in Go

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?

Upvotes: 39

Views: 36601

Answers (5)

ttt
ttt

Reputation: 6809

The difference is only in the code syntax that you write to print something.

fmt.Printf() allows formatted printing, meaning you can use placeholders (format specifiers) to control the output.

Example:

const alice := "Alice"
const number := 5
fmt.Printf("Hello, %s! You have %d messages.\n", alice, number)

With fmt.PrintLn() you write it differently and it adds a newline at the end of the output.

Example:

const alice := "Alice"
const number := 5
fmt.PrintLn("Hello, " + alice + "! You have " + number + " messages.")

Upvotes: 0

Nate Schreiner
Nate Schreiner

Reputation: 849

  • 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.

Upvotes: 54

user12817546
user12817546

Reputation:

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.

Upvotes: 0

Pierre B
Pierre B

Reputation: 15

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

Upvotes: -2

robert
robert

Reputation: 811

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.

Upvotes: 25

Related Questions