Home >Backend Development >Golang >What's the Difference Between `Print`, `Println`, and `Printf` in Go?

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

Patricia Arquette
Patricia ArquetteOriginal
2024-11-13 11:32:02362browse

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")

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

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

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

The above is the detailed content of What's the Difference Between `Print`, `Println`, and `Printf` in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn