ADVERTISEMENT
ADVERTISEMENT

Golang fmt.Print() Function with Examples

Golang | fmt.Print() Function: Here, we are going to learn about the Print() function of the fmt package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 08, 2021

fmt.Print()

In Go language, the fmt package implements formatted I/O with functions analogous to C's printf() and scanf(). The Print() function is an inbuilt function of the fmt package which is used to format using the default formats for its operands and writes to standard output. If there is no string, then the spaces are added between operands.

It accepts two parameters (r io.Reader, a ...interface{}) and returns the number of total bytes written and an error if occurred during the write operation.

Syntax:

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

Parameter(s):

  • a : A custom type that is used to specify a set of one or more method signatures, here we can provide a set of the variables, constants, functions, etc.

Return Value:

The return type of the fmt.Print() function is (n int, err error), it returns the number of total bytes written and an error if occurred during the write operation.

Example 1:

// Golang program to demonstrate the
// example of fmt.Print() function

package main

import (
	"fmt"
)

func main() {
	// Printing simple text
	n, err := fmt.Print("Hello, world!")

	// Prints newline
	fmt.Println()

	// fmt.Print() returns:
	// n - Number of printed characters
	// err - Error (if any)
	fmt.Println(n, "Characters printed.")
	fmt.Println("Error: ", err)
}

Output:

Hello, world!
13 Characters printed.
Error:  <nil>

Example 2:

// Golang program to demonstrate the
// example of fmt.Print() function

package main

import (
	"fmt"
)

func main() {
	// Print text with new line
	fmt.Print("Hello World\n")
	fmt.Print("Hi, there...\n")

	// Printing text, values together
	fmt.Print("Name: ", "Alex", " Age: ", 21, "\n")

	// Printing variable values

	// Declaring & assigning variables
	var (
		name string
		age  int
		perc float32
	)

	name = "Alex"
	age = 21
	perc = 87.5

	// Printing
	fmt.Print("Name: ", name, " Age: ", age, " Perc: ", perc, "\n")
}

Output:

Hello World
Hi, there...
Name: Alex Age: 21
Name: Alex Age: 21 Perc: 87.5

Example 3:

// Golang program to demonstrate the
// example of fmt.Print() function

package main

import (
	"fmt"
)

func main() {
	name := "Dev"
	age := 21
	city := "New York"

	// Printing using fmt.Print()
	fmt.Print("Hey I'm ", name, " ", age, " years old.\n")
	fmt.Print("I live in ", city, "\n")
}

Output:

Hey I'm Dev 21 years old.
I live in New York

Golang fmt Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.