Skip to main content

Let’s talk about the various functions & ways of using the fmt.Package!

The fmt package is one of the most essential in the Go programming language. It keeps a record of file formation. It helps in printing the console.

In fact, You can always hire golang developers in order to use these fmt package functions. 

Fmt package also unfolds a wide variety of functions of data such as string, Integers & booleans, etc.

The “term” package is simply defined as a basic collection of files that are meant to be executed together.

Similarly, the fmt package functions with the inputs, outputs, values & multiple strings as well. 

What are the Functions of the fmt package in Golang?

There are various functions of the fmt package in the Golang Programme language. The distinctive codes & it’s running are mentioned below –

fmt.Print

fmt.Print function is primarily used to print the text to the console.

In this example, we are printing the string “hello world”.

Let’s take a look at this code to grab the concept earliest.

package main

import “fmt”

func main(){

   fmt.Print(“Hello world”)

}

Hence, the output for this will be Hello World

fmt.Printf

fmt.Printf works in the basic functioning of string interpolation. fmt.Print formats the string in a manner the developer wanted them to be.

Moreover, the detailed specification of fmt.Printf is represented by percentages & characters. Under the Go programming language, we call them “verbs”.

Hence, some common verbs are as follows –

     PercentageCharacter
          %s               string
          %d               Integers
          %vAny value
          %ffloat

Now, let’s take a look at how it’ll look in code. 

package main

import “fmt”

func main() {

    const name, age = “Rohan Singh”, 45

    fmt.Print(name, age)

    fmt.Print(“My name is Rohan singh and my age is 45”)

    fmt.Printf(“My name is %s and my age is %d”, name, age)

}

Above mentioned are the codes for fmt.Printf and let’s see the outputs for the same.

fmt.Println

Apparently, fmt.Println works similarly to fmt.Print but there are some subtle changes in the overall formatting and its functioning itself.

fmt.Println helps to print the text to the console in the next line.             {Note : ln = line} 

It prints the inserted string input. Further, the cursor moves to the following line. That’s what fmt.Println does.

Now, Let’s see how it’ll work in code.

package main

import “fmt”

func main() {

    const name, empid = “Muskaan Khanna”,23

    fmt.Println(name, empid)

    fmt.Println(“My name is Muskaan khanna and my empid is 23”)

}

Above mentioned are the codes for fmt.Println and let’s see the outputs for the same.

Hence, As you can witness that fmt.Println helps to print the text to the console in the new line.

fmt.Sprint

To begin with, the overall function & characteristics of fmt.Sprint, will see there slightly a difference between fmt.print & fmt.Sprint.

Basically, fmt.sprint does not print the text to the console. It’s simply an opposite version of fmt.Print. It returns the input string & doesn’t reciprocate.

fmt.Sprintf

The fmt.Sprintf returns the formatted string. The Sprint(f) functions the same way fmt.Print(f) does but there is an exception in that it returns a string that can be eventually stored in a form of a variable.

So, let us begin with how fmt.Sprint(f) will look in the terminal of VS code.

package main

import “fmt”

func main() {

    const name, age = “Rahul Mehra”, 28

    m := fmt.Sprintf(“%s\n is %d\n years old”, name, age)

    fmt.Println(m)

}

Now, let’s see the output for the same.

fmt.Sprintln

In the Go programme language,fmt.Sprintln generates & returns the resulting string. It also automatically adds up the space to the data type values. A brand-new line also adds up toward the end of the formatted string.

Now, let’s see how fmt.Sprintln work in a VS code terminal.

package main

import “fmt”

func main() {

    const name, age = “Rahul Mehra”, 28

    d := fmt.Sprintln(“my name is Rahul Mehra and I am 28 years old”)

    fmt.Println(d)

}

Let’s take a look at the output of the above code in the VS code terminal.

fmt.Scan

The basic functions of fmt.Scan is to scan the input text that is given into the standard input. In addition to this, this function is defined under the fmt package. You have to import the “fmt” to make use of these functions.

fmt.Scan returns the number of items that are successfully scanned. Also, here newlines are counted as space.

Now, Let’see how it’ll look in VS code editor terminal.

package main

import (

    “fmt”

)

// Calling main

func main() {

    var name string

    fmt.Println(“what is your name?”)

    fmt.Scan(&name)

    fmt.Print(“My name is “, name)

}

Let’s see the output.

fmt.Scan(f)

The fmt.Scan(f) is primarily used to read the data from the standard inputs. Afterwards, It is formatted into the strings.

Fmt.Scan(f) is already an inbuild function of the fmt.package. Generally, it returns the number of items which are generally scanned.

       Percentage    Character
            %s         String
            %d         Integers
             %v        Any Value
             %+v Any value + name

Now, let’s take a look at how fmt.Scan(f) will look in VS code terminal.

package main

//

import (

    “fmt”

)

// Calling main

func main() {

    var name string

    fmt.Println(“what is your name?”)

    fmt.Scanf(“%s”, &name)

    fmt.Printf(“My name is %s.”, name)

}

Now, Let’see the output.

fmt.Scanln

The fmt.Scanln function is an inbuild function of the Go programming language.

The fmt.Scanln function is similar to fmt.Scan but there are some differences such as it stops the scanning at the new line.

package main

import (

    “fmt”

)

// Calling main

func main() {

    var name string

    var age int

    fmt.Println(“what is your name?”)

    fmt.Scanln(&name)

    fmt.Println(“what is your age”)

    fmt.Scanln(&age)

    fmt.Printf(“My name is %s and age is %d”, name, age)

}

Now Let’s see how it’ll look in code.

Final Words!

To conclude the whole article, we can say that fmt.Package is said to be one of the important parts of the Go programming language. It offers a wide range of functions & formatting etc. Which helps the console to the file.

fmt.Package supports and helps to print a wide range of varieties of data such as strings, integers and others. Although In the above article, we have witnessed some common functions such as fmt.Print,fmt.Printf,fmt.Println,fmt.Sprint,fmt.Sprintf,fmt.Sprintln,fmt.Scan and fmt.Scanln etc.

Happy Reading!