Reputation: 11830
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?
Upvotes: 39
Views: 36601
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
Reputation: 849
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 Print()
however it will
append a newline character \n
at the end.Upvotes: 54
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
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
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