Golang program to demonstrate the string interpolation


In Go programming language, string interpolation is the process of integrating expressions into a string literal. When it's necessary to include dynamic values in strings for log statements, error messages, and other purposes, string interpolation is frequently used. We will use two methods in this program to demonstrate string interpolation. The first example demonstrates use of sprintf and the second example shows how to use printf to execute string interpolation.

Method 1: Using sprintf with fmt package

In this method, the name and age variables' values are combined into a string using fmt.Sprintf() function which demonstrate interpolation.  Here, Strings are represented by the placeholder %s, while numbers are represented by the placeholder %d. Let’s see through the code and algorithm to understand the concept.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function create a variable name and age.

  • Step 3 − Call the fmt.Sprintf function, passing the arguments name and age along with a format string.

  • Step 4 − The name and age values are substituted for placeholders in the format string to produce a string by the fmt.Sprintf function.

  • Step 5 − Using fmt.Println, print the finished string to the console.

  • Step 6 − This algorithm exemplifies the use of string interpolation in Go, which enables the dynamic inclusion of variable values in a string at runtime.

Example

In this example we will use sprintf function to demonstrate string interpolation. Let’s see through the code to know how the execution is taking place.

package main
import (
   "fmt"    //import fmt package
)

//create main function to execute the program
func main() {
   name := "Ritika"  //create name string
   age := 21   //create age variable 
   fmt.Println("The string interpolation can be demonstrated as:")
   message := fmt.Sprintf("Hello, my name is %s and I am %d years old", name, age) //using sprintf function combine the string  
   fmt.Println(message) //print the combined string on the console 
}

Output

The string interpolation can be demonstrated as:
Hello, my name is Ritika and I am 21 years old

Method 2: Using printf with fmt package

In this example the name and age variables' values are printed as a string using the fmt.Printf function. Like in the fmt.Sprintf example, the format string "Hello, my name is %s and I am %d years old" specifies how the values should be interpolated into the string. Strings are represented by the placeholder %s, while numbers are represented by the placeholder %d. The distinction is that fmt.Printf prints the output string to the console directly rather than returning it.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Declare name and age variables and their corresponding values.

  • Step 3 − Call the fmt.Printf function while providing a format string, name, and age values as arguments.

  • Step 4 − The name and age values are substituted for placeholders in the format string by the fmt.Printf function, which then prints the finished string to the console.

  • Step 5 − The fmt.Printf function is used in this algorithm to include the values of variables in a string and print the outcome directly to the console. This method of string interpolation in Go is demonstrated.

Example

In this example we will use printf function to demonstrate string interpolation. Let’s see through the code.

package main
import (
   "fmt"   //import fmt package in the program
)

//create main function to execute the program
func main() {
   name := "Ritika"  //create a name string
   age := 21 //create an age variable
   
   fmt.Println("The interpolation of string is demonstrated as:")
   fmt.Printf("Hello, my name is %s and I am %d years old\n", name, age) //using printf function   combine the strings and print it on the console
}

Output

The interpolation of string is demonstrated as:
Hello, my name is Ritika and I am 21 years old

Conclusion

We executed the program of demonstrating the string interpolation using two methods. In the first method we used the sprintf function with fmt package and in the second example we used printf function with fmt package.

Updated on: 20-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements