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