go-tutorial

Go Print Statement

These two functions are used to print output messages on the screen in Go programming.

  • fmt.Print()
  • fmt.Println()

Note that all of these functions are part of the fmt package. Before we can utilise these functions, we must first import the fmt package.

Go fmt.Print()

Let’s take an example.

// Program to illustrate fmt.Print()
 package main
// importing the fmt 
package import ("fmt") 
func main() {
fmt.Print("Hello, ")
fmt.Print("World!")
}

Output

Hello World!

Here, the fmt.Print() function prints the content inside parentheses ().

Go fmt.Println()

The way fmt.Println() works is similar to how fmt.Print() works with a couple of differences.

  • fmt.Println() prints a new line at the end by default.
  • If we print multiple values and variables at once, a space is added between the values by default.
// Program to illustrate the use of Println() function 
package main
import ("fmt")
// prints output in different lines 
func main() {
currentSalary := 50000
fmt.Println("Hello") 
fmt.Println("World!")
fmt.Println("Current Salary:", currentSalary)
}

Output:

Hello 
World!
Current Salary: 50000

Things to notice:

  • All the output messages are printed in separate lines
  • A space is added after Current Salary: by default

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *