SoFunction
Updated on 2025-03-01

This article will help you understand the use of the output function of the Go language fmt standard library

fmt output function

function describe
Fprint(w , a ...any) (n int, err error) Write parameters into w using the default format, and if neither adjacent parameters are strings, add spaces before them
Fprintf(w , format string, a ...any) (n int, err error) Format according to the format specifier and write to w
Fprintln(w , a ...any) (n int, err error) Use the default format to write parameters to w, adding spaces and line breaks between parameters
Print(a ...any) (n int, err error) Print the specified parameters. If neither adjacent parameters are strings, separate them with spaces when printing.
Printf(format string, a ...any) (n int, err error) Format and print according to the format specifier
Println(a ...any) (n int, err error) Print the specified parameters, separated by spaces and newlines
Sprint(a ...any) string Format the parameters in the default format. If neither adjacent parameters are strings, add spaces in the middle and return as the result of the string.
Sprintf(format string, a ...any) string Format according to the format specifier and return as the result of the string
Sprintln(a ...any) string Format the parameters according to the default format, add spaces and line breaks between the parameters, and return as the result of the string.

Fprint、Fprintf、Fprintln

import (
    "fmt"
    "os"
)

func main() {
    file, err := ("./", os.O_WRONLY, 0666)
    if err != nil {
        return
    }
    defer ()

    (file, "123")
    (file, "456")
    (file, "%d", 789)
}

OpenFile, viaFprintThe function first123Write to the file;

Then passFprintlnThe function will456Write to the file and wrap the line;

Last passedFprintfThe function will789Format output to file.

The contents of the file after the program is:

123456
789

Print、Printf、Println

import (
    "fmt"
)

func main() {
    ("Hello, ")
    ("World!")
    s := "golang!"
    ("Hello, %s", s)
}

First passPrintFunction PrintingHello, ;

Then passPrintlnFunction PrintingWrold!and line-breaking, connecting with the sentence printed above, the printed content of one line isHello, World!;

Last passedPrintffunction, format and print.

The final print result is:

Hello, World!
Hello, golang!

Sprint、Sprintf、Sprintln

import (
    "fmt"
)

func main() {
    s1 := ("hello, world!")
    ("%#v\n", s1)
    s2 := ("hello, gopher!")
    ("%#v\n", s2)
    name := "Xiao Ming"
    s3 := ("hello, %s", name)
    ("%#v\n", s3)
}
  • passSprintConvert the specified content to a string in the default format and use variabless1Receive, through placeholders%#vPrint outs1Native content →"hello, world!"
  • passSprintlnConvert the specified content to a string in the default format and add a newline character, using variabless2Receive, through placeholders%#vPrint outs2Native content →"hello, gopher!\n"
  • passSprintfConvert the specified content to a string of the specified format and use variabless3Receive, through placeholders%#vPrint outs3Native content →"hello, Xiao Ming"
  • If you don't know much about placeholders, you can check out this articleIn one article, you can learn about the common placeholders and their simple use of the Go fmt standard library

summary

This article is correctfmtCommon output functions of the standard library are introduced. The output function is introduced in three categories. The first category is output to the output stream, the second category is standard output, and the third category is output to the string. And give simple example code.

This is the article about this article that will introduce you to the use of the output functions of the Go language fmt standard library. For more related content of the Go language fmt standard library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!