Interpolation in Go

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Go

Interpolation means inserting a variable value or expression into a string. The simplest way to interpolate strings in Go is by using the fmt.Sprintf() function or the strings.Replace() function.

Here are examples of both the approaches:

1. Using fmt.Sprintf():

   package main

import "fmt"

func main() {
name := "John"
age := 30
message := fmt.Sprintf("My name is %v and I am %v years old.", name, age)
fmt.Println(message)
}


Output: My name is John and I am 30 years old.

2. Using strings.Replace()

   package main

import "strings"

func main() {
name := "John"
age := 30
messageTemplate := "My name is {name} and I am {age} years old."
message := strings.Replace(messageTemplate, "{name}", name, 1)
message = strings.Replace(message, "{age}", fmt.Sprintf("%v", age), 1)
fmt.Println(message)
}


Output: My name is John and I am 30 years old.

In the second approach, we used a message template with placeholders, and then replaced them with actual values using the strings.Replace() function. This approach is useful when you have a fixed message template and want to insert variable values into it.