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
Release: 2024-11-13 11:32:02
Original
335 people have browsed it

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")
Copy after login

Output:

Hello World
Copy after login
Copy after login

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")
Copy after login

Output:

Hello World
Copy after login
Copy after login

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")
Copy after login

Output:

HelloWorld
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template