Golang fmt.Println() Function with Examples (2024)

  • Home
  • DS & Algo. ▾
    • Data Structure
    • Algorithms
    • Coding Problems
  • Languages ▾
    • C
    • C++
    • C++ STL
    • Java
    • Python
    • Scala
    • Ruby
    • C#.Net
    • Golang
    • Android
    • Kotlin
    • SQL
  • Web. ▾
    • JavaScript
    • CSS
    • jQuery
    • PHP
    • Node.Js
    • AdonisJs
    • VueJS
    • Ajax
    • HTML
    • Django
  • Programs ▾
    • C
    • C++
    • Data Structure
    • Java
    • C#.Net
    • VB.Net
    • Python
    • PHP
    • Golang
    • Scala
    • Swift
    • Rust
    • Ruby
    • Kotlin
    • C Interview Programs
  • Aptitude ▾
    • C Aptitude
    • C++ Aptitude
    • Java Aptitude
    • C# Aptitude
    • PHP Aptitude
    • Linux Aptitude
    • DBMS Aptitude
    • Networking Aptitude
    • AI Aptitude
    • More...
  • Interview ▾
    • Golang
    • MIS Executive
    • DBMS
    • C
    • Embedded C
    • Java
    • SEO
    • HR
  • Find Output ▾
    • C
    • C++
    • C#.Net
    • Java
    • Go
    • PHP
    • More...
  • MCQs ▾
    • Web Technologie MCQs
    • CS Subjects MCQs
    • Databases MCQs
    • Programming MCQs
    • Testing Software MCQs
    • Digital Mktg Subjects MCQs
    • Cloud Computing S/W MCQs
    • Engineering Subjects MCQs
    • Commerce MCQs
    • More MCQs...
  • CS Subjects ▾
    • Machine Learning/AI
    • Operating System
    • Computer Network
    • Software Engineering
    • Discrete Mathematics
    • Digital Electronics
    • Data Mining
    • MIS
    • DBMS
    • Embedded Systems
    • Cryptography
    • CS Fundamental
    • More Tutorials...
  • More ▾
    • Tech Articles
    • Puzzles
    • Full Forms
    • Code Examples
    • Blogs
    • Guest Post
    • Programmer's Calculator
    • XML Sitemap Generator
    • About
    • Contact

×

Golang Tutorial

  • Golang - Home
  • Golang - Keywords
  • Golang - Data Types
  • Golang - Variables
  • Golang - Constants
  • Golang - Program Structure
  • Golang - Comments
  • Golang - Naming a variable
  • Golang - Shorthand for Defining Multiple Variables/Constants
  • Golang - Find Type of Variable
  • Golang - Binary Literals
  • Golang - Octal Literals
  • Golang - Hexadecimal Literals
  • Golang - Type Casting or Type Conversion
  • Golang – Operators
  • Golang - Arithmetic Operators
  • Golang - Relational Operators
  • Golang - Logical Operators
  • Golang - Bitwise Operators
  • Golang - Assignment Operators
  • Golang - Miscellaneous Operators
  • Golang - if statement
  • Golang - if-else statement
  • Golang - if-else-if ladder statement
  • Golang - nested if statement
  • Golang - switch statement
  • Golang - select statement
  • Golang - Loops
  • Golang - Functions
  • Golang - Strings

Golang Reference

  • Go - builtin Package
  • Go - bytes Package
  • Go - fmt Package
  • Go - math Package
  • Go - os Package
  • Go - sort Package
  • Go - strconv Package
  • Go - strings Package
  • Go - unicode Package

Golang Programs

  • Golang Programs - Home
  • Golang - Basic Programs
  • Golang - Switch Statement Programs
  • Golang - goto Statement Programs
  • Golang - Looping Programs
  • Golang - Array Programs
  • Golang - String Programs
  • Golang - Structure Programs
  • Golang - User-defined Function Programs
  • Golang - Variadic Function Programs
  • Golang - Recursion Programs
  • Golang - Pointer Programs
  • Golang - Conversion Programs
  • Golang - Slices Programs
  • Golang - Goroutines Programs
  • Golang - Reflection Programs
  • Golang - Maps Programs
  • Golang - Range Programs
  • Golang - Channels Programs
  • Golang - File Handling Programs
  • Golang - Buffered Input-Output Programs
  • Golang - Command-line Arguments Programs
  • Golang - Regular Expressions Programs
  • Golang - JSON Programs
  • Golang - os Package Programs
  • Golang - strconv Package Programs
  • Golang - log Package Programs
  • Golang - math/rand Package Programs
  • Golang - math/bits Package Programs
  • Golang - sync/atomic Package Programs
  • Golang - path/filepath Package Programs
  • Golang - syscall Package Programs
  • Golang - Signals Programs
  • Golang - Shell Script Programs

Golang Practice

  • Golang Interview Questions
  • Golang Programs
  • Golang Find Output Programs
  • Golang Aptitude Questions
  • Go FAQ

Golang Miscellaneous

  • Golang - Print Boolean Value
  • Golang - Print Double-quoted String
  • Golang - Convert From Int to Binary
  • Golang - Convert From Int to Octal
  • Golang - Convert From Int to Hex
  • Golang - Check if Structure is Empty
  • Golang - Check if Key Exists in Map
  • Golang - Return an Error

Home »Golang »Golang Reference

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() functionpackage mainimport ("fmt")func main() {// Printing simple textn, 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() functionpackage mainimport ("fmt")func main() {// Print text with new linefmt.Println("Hello World")fmt.Println("Hi, there...")// Printing text, values togetherfmt.Println("Name: ", "Alex", " Age: ", 21)// Printing variable values// Declaring & assigning variablesvar (name stringage intperc float32)name = "Alex"age = 21perc = 87.5// Printingfmt.Println("Name: ", name, " Age: ", age, " Perc: ", perc)}

Output:

Hello WorldHi, there...Name: Alex Age: 21Name: Alex Age: 21 Perc: 87.5

Example 3:

// Golang program to demonstrate the// example of fmt.Println() functionpackage mainimport ("fmt")func main() {name := "Dev"age := 21city := "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 »

Golang fmt.Printf() Function

Golang fmt.Scan() Function

Comments and Discussions!

Load comments ↻


Golang fmt.Println() Function with Examples (2024)

FAQs

What does fmt println do in Golang? ›

The Go fmt package supports two closely-related functions for formatting a string to be displayed on the terminal. . Print() accepts strings as arguments and concatenates them without any spacing. . Println() , on the other hand, adds a space between strings and appends a new line to the concatenated output string.

What is the difference between Println and FMT? ›

Println does not support custom format specifiers, which means only the default formats are used to format the string . fmt. Println also appends a newline character at the end of the string.

What is the function of FMT print? ›

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.

What is use of fmt command? ›

The fmt command is generally used to format mail messages to improve their appearance before they are sent. However, the fmt command may also be useful for simple formatting tasks. For example, within visual mode of a text editing program such as the vi editor, the command !}

How to print in Go without fmt? ›

Printing Without Package

Here, we have used println() and print() instead of fmt. Println() and fmt. Print() respectively. Note: It's recommended to use the fmt package for printing.

What does Println () do? ›

println is short for "print line", meaning after the argument is printed then goes to the next line. printf is short for print formatter, it gives you the ability to mark where in the String variables will go and pass in those variables with it. This saves from having to do a long String concatenation.

What is the difference between Println and print in Golang? ›

Print and Println are the same with one thing that differs, the ln. It of course stands for line. So print, simply prints whatever you want it to print. Print line adds a new line at the end.

How to run Go.fmt on a file? ›

If you want to use the gofmt anyway, you can use the following tools: Go tools: click Tools | Go Tools and select Go Fmt File (to run gofmt in the current file) or Go Fmt Project (to run gofmt for the current project).

How do you pronounce fmt in Golang? ›

Before we go any further, I should note that the “fmt” package is pronounced “fumpt”.

What is %+ v in Golang? ›

General: %v the value in a default format when printing structs, the plus flag (%+v) adds field names %#v a Go-syntax representation of the value %T a Go-syntax representation of the type of the value %% a literal percent sign; consumes no value. Boolean: %t the word true or false.

What is the print function in Golang? ›

Print() is a variadic function, which means that it takes a variable number of input arguments. The functionality of Print in Golang is similar to cout in C++ or printf in C. Note: The Print() function does not append a new line at the end of the input data stream. However, it can be added explicitly by using '\n' .

Is the fmt header-only? ›

If desired, fmt may be built as a static library, or as a header-only library.

What does fmt stand for? ›

Fecal microbiota transplantation (FMT) is a procedure that delivers healthy human donor stool to a child via colonoscopy, enema, nasogastric (NG) tube, or in capsule form (popularly called “poop pills”). It may be prescribed for debilitating gasterointestinal infections, such as Clostridium difficile (C.

What is the difference between Golint and Go fmt? ›

gofmt - Start with the standard Go code formatter. golint - Detects style mistakes in Go code (deprecated).

What is := in Go? ›

The := operator is known as the short variable declaration operator in Go. It's a shorthand for declaring a new variable and assigning a value to it in a single step.

What does Fprintf do in Go? ›

The Fprintf function in the Go programming language is used to print out a formatted string to the destination of your choosing. Fprintf is different from the normal Fprint function, as it supports custom format specifiers and uses a format string to generate the final output string.

Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6065

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.