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, via
Fprint
The function first123
Write to the file;
Then passFprintln
The function will456
Write to the file and wrap the line;
Last passedFprintf
The function will789
Format 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 passPrint
Function PrintingHello,
;
Then passPrintln
Function PrintingWrold!
and line-breaking, connecting with the sentence printed above, the printed content of one line isHello, World!
;
Last passedPrintf
function, 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) }
- pass
Sprint
Convert the specified content to a string in the default format and use variabless1
Receive, through placeholders%#v
Print outs1
Native content →"hello, world!"
; - pass
Sprintln
Convert the specified content to a string in the default format and add a newline character, using variabless2
Receive, through placeholders%#v
Print outs2
Native content →"hello, gopher!\n"
; - pass
Sprintf
Convert the specified content to a string of the specified format and use variabless3
Receive, through placeholders%#v
Print outs3
Native 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 correctfmt
Common 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!