Apakah Perbezaan Antara `Cetak`, `Println` dan `Printf` dalam Go?

Patricia Arquette
Lepaskan: 2024-11-13 11:32:02
asal
269 orang telah melayarinya

What's the Difference Between `Print`, `Println`, and `Printf` in Go?

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")
Salin selepas log masuk

Output:

Hello World
Salin selepas log masuk
Salin selepas log masuk

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")
Salin selepas log masuk

Output:

Hello World
Salin selepas log masuk
Salin selepas log masuk

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

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")
Salin selepas log masuk

Output:

HelloWorld
Salin selepas log masuk

Summary

  • Println: Prints arguments with newline.
  • Printf: Allows formatting and provides more control over output.
  • Print: Prints arguments as is, without formatting or line breaks.

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!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan