Understanding Print, Println, and Printf in Go
A programmer from the world of JavaScript seeks to understand the differences between three printing functions in Go: Print, Println, and Printf. In Go, the instructor used Printf to determine the type of a variable instead of Println.
Println
Println, as its name suggests, prints its arguments to the standard output in a line and appends a newline character at the end. For example:
fmt.Println("Hello", "World")
Output:
Hello World
Printf
Printf, also known as "Print Formatter," allows you to format variables, numbers, and strings before printing them. It uses a format string, which specifies how the arguments are formatted, as the first parameter. For example:
fmt.Printf("%s %s", "Hello", "World")
Output:
Hello World
In this case, the format string %s indicates that we want to print a string. You can use various other format specifiers to represent different data types.
Print simply prints its arguments to the standard output in the order they are given, without any formatting or line breaks. For example:
fmt.Print("Hello") fmt.Print(" ") fmt.Print("World")
Output:
HelloWorld
Summary
Atas ialah kandungan terperinci Apakah Perbezaan Antara `Cetak`, `Println` dan `Printf` dalam Go?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!