Open In App

fmt.print() Function in Golang With Examples

Last Updated : 05 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.print() function in Go language formats using the default formats for its operands and writes to standard output. Here Spaces are added between operands when any string is not used as a parameter. Moreover, this function is defined under the fmt package. Here, you need to import the “fmt” package in order to use these functions.

Syntax:

func Print(a ...interface{}) (n int, err error)

Here, “a …interface{}” containing some strings and declared constant variables.

Return Value: It returns the number of bytes written and any write error encountered.

Example 1:




// Golang program to illustrate the usage of
// fmt.print() function
  
// Including the main package
package main
  
// Importing fmt
import (
    "fmt"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const name, dept = "GeeksforGeeks", "CS"
  
    // Calling print() function
    fmt.Print(name, " is a ", dept, " portal.\n")
  
    // It is conventional not to worry about any
    // error returned by Print.
  
}


Output:

GeeksforGeeks is a CS portal.

Example 2:




// Golang program to illustrate the usage of
// fmt.print() function
  
// Including the main package
package main
  
// Importing fmt
import (
    "fmt"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const str1, str2, str3 = "a", "b", "c"
  
    // Calling print() function
    fmt.Print(str1, str2, str3, "\n")
  
    // It is conventional not to worry about any
    // error returned by Print.
  
}


Output:

abc

In the above code, the constant variables used are strings hence spaces are not added in between two strings shown above in the output.

Example 3:




// Golang program to illustrate the usage of
// fmt.print() function
  
// Including the main package
package main
  
// Importing fmt
import (
    "fmt"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const num1, num2, num3 = 5, 15, 15
  
    // Calling print() function
    fmt.Print(num1, num2, num3, "\n")
  
    // It is conventional not to worry about any
    // error returned by Print.
  
}


Output:

5 15 15

In the above code, the constant variables used are numbers hence spaces are added in between two numbers shown above in the output.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads