The Print function in the fmt package is used to write formatted output to the standard output, which is typically the console or terminal where your program is running. However, sometimes you may want to write formatted output to a different destination, such as a file or a network connection. That's where the Fprint function comes in.
The Fprint function allows you to specify an io.Writer as the first argument, which can be any type that implements the io.Writer interface. This enables you to write the formatted output to that specific destination instead of the standard output.
Here's an example to illustrate the usage of Fprint:
In this example, the program creates a file named "output.txt" using os.Create. Then, it uses Fprint to write the string "Hello, world!" to the file using the file variable as the first argument.
Similarly, Sprint is useful when you want to format and store the output in a string variable instead of immediately writing it to the standard output or any other io.Writer. It returns the formatted string as a result, which you can then assign to a variable for further use or manipulation.
Here's an example illustrating the usage of Sprint:
In this example, Sprint is used to concatenate the strings "Hello, " and "world!" into a single string and store it in the message variable. Later, the message variable is printed using Println to display the output.
In summary, while Print is used to write formatted output to the standard output, Fprint allows you to write to a specific io.Writer and Sprint returns the formatted string as a result. These additional functions provide flexibility and options for directing and storing formatted output according to your specific needs.