SoFunction
Updated on 2025-03-03

Analysis of the difference between Print Printf and Println in Go

one,PrintandPrintln

These two printing methods are similar, and there are only differences in formats, as follows:

package main

import "fmt"

func main() {
    ("hello")
    ("world")
    ("========")
    ("hello")
    ("world")
    ("========")
    ("hello", "world")
    ("hello", "world")
}

// The results are as follows:helloworld========
hello
world
========
helloworldhello world

From the above we can see:

  • Println There are blank lines between each item printed, Print does not
  • Println There will be spaces between each of them when printing multiple, Print does not

two,PrintlnandPrintf

as follows:

package main

import "fmt"

func main() {
    a:= 10
    b:= "hello, world!"
    ("%d,%v", a, b)
    ("%d,%v", a, b)
}

// The results are as follows:%d,%v 10 hello, world!
10,hello, world!% 

Printf can print out formatted strings, Println cannot

Summarize:

function Multiple items output in the same function Different function output
Println There are spaces between Line break
Print No space exists No line break
Printf Format output No line break
  • Println and Printf are both public methods in the fmt package. These two functions are required when printing information.
  • Println: Can print out strings and variables
  • Printf: Only formatted strings can be printed, string type variables can be output, and plastic variables and plastic variables can not be output.
  • Println will automatically wrap lines, Print will not
  • Println There are blank lines between each item printed, Print does not

1、/

2、/yuguog/p/

This is the end of this article about the difference between Print Printf and Println in Go. For more information about the difference between Print Printf and Println in Go, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!