The fmt.Print
function in the GO programming language is a function used to print out a formatted string
to the console. fmt.Print
does not support custom format specifiers, which means only the default formats are used to format the string
.
Here, Print
is the actual function while fmt
is the GO package that stores the definition of this function. So to use this function, you must import the fmt
package in your file and access the Print
function within using the .
notation: fmt.Print
The definition of the Print
function inside the fmt
package is as follows:
fmt.Print
takes a variable number of arguments. In the function definition above, a ...interface{}
refers to the list of all arguments that need to be formatted and printed.
The fmt.Print
function can return two things:
count
: The number of bytes that were written to the standard output.
err
: Any error thrown during the execution of the function.
The following example is a simple program where we print out a single string
along with an integer value:
package mainimport ("fmt")func main() {// declaring variables of different datatypesvar message string = "Hello and welcome to "var year int = 2021// printing out the declared variables as a single stringfmt.Print(message, year)}
This example shows you how you can make use of the return values from the fmt.Print
function:
package mainimport ("fmt")func main() {// declaring variables of different datatypesvar message string = "Hello and welcome to Educative \n"// storing the return values of Printchar_count, error := fmt.Print(message)if error == nil {fmt.Print("Print executed without errors and wrote ",char_count, " characters")}}
RELATED TAGS
CONTRIBUTOR