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?
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 .
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.
Printf
- "Print Formatter" this function allows you to format numbers, variables and strings into the first string parameter you give itPrintln
- "Print Line" This cannot format anything, it simply takes a string, prints it and append a newline character,
Print
- "Print" same thing as Println()
however it will NOT append a newline characterfmt.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