ADVERTISEMENT
ADVERTISEMENT

Golang fmt.Println() Function with Examples

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

fmt.Println()

In Go language, the fmt package implements formatted I/O with functions analogous to C's printf() and scanf(). The Println() 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. Spaces are always added between operands and a newline is appended.

It accepts one parameter (a ...interface{}) and returns the number of total bytes written and an error if occurred during the write operation.

Syntax:

func Println(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.Println() 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.Println() function

package main

import (
	"fmt"
)

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

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

Output:

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

Example 2:

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

package main

import (
	"fmt"
)

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

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

	// Printing variable values

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

	name = "Alex"
	age = 21
	perc = 87.5

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

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.Println() function

package main

import (
	"fmt"
)

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

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

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.