🧵 Go String Interpolation – Complete Guide (2025 Edition)
Learn how to insert variables into strings in Go (Golang) using powerful formatting techniques.
🚀 Introduction – What Is String Interpolation in Go?
String Interpolation refers to the process of inserting variables or expressions directly into a string. While Go does not support string interpolation in the traditional sense (like JavaScript or Python), it offers powerful alternatives using fmt package functions such as:
fmt.Sprintf()fmt.Printf()
✅ These methods allow you to format strings dynamically by combining variables and text.
📌 Go String Interpolation Using fmt.Sprintf()
✅ Basic Syntax
import "fmt"
name := "Vaibhav"
message := fmt.Sprintf("Hello, %s!", name)
fmt.Println(message) // Output: Hello, Vaibhav!
🔹 Format Specifiers:
| Specifier | Description |
|---|---|
%s | String |
%d | Decimal (int) |
%f | Float |
%t | Boolean |
%v | Any value (default) |
🔍 Examples of Go String Interpolation
🔹 Interpolating Multiple Variables
age := 30
name := "Vaibhav"
msg := fmt.Sprintf("%s is %d years old.", name, age)
fmt.Println(msg) // Output: Vaibhav is 30 years old.
🔹 Formatting Numbers
price := 49.99
result := fmt.Sprintf("The price is $%.2f", price)
fmt.Println(result) // Output: The price is $49.99
🔹 Using %v for Generic Formatting
x := true
fmt.Printf("Value: %v\n", x) // Output: Value: true
🧩 Alternative: fmt.Printf() for Direct Output
Instead of returning a string, fmt.Printf() prints it directly to the console.
name := "GoLang"
fmt.Printf("Welcome to %s programming!\n", name)
📌 Use
Printf()for formatted printing, andSprintf()for formatted string generation.
🧠 Advanced String Formatting
🔹 Padding and Alignment
fmt.Printf("|%-10s|%10s|\n", "Left", "Right")
🔹 Data Structure Formatting
user := struct {
Name string
Age int
}{"Alex", 28}
fmt.Printf("User Info: %+v\n", user)
✅ Use Cases of String Interpolation in Go
| Use Case | Example |
|---|---|
| Console Logging | fmt.Printf() for readable output |
| Dynamic HTML templates | Insert data into HTML templates |
| API Message Formatting | Create readable JSON or error logs |
| File and Report Writing | fmt.Sprintf() to format content |
🧾 Summary
Although Go doesn’t support direct string interpolation like some other languages, it provides powerful formatting tools using the fmt package:
fmt.Sprintf()– returns a formatted stringfmt.Printf()– prints a formatted string- Format specifiers let you control output precisely
💡 Tip: Use
%vfor general formatting, and%+vor%#vfor struct introspection in debugging.
❓ Frequently Asked Questions (FAQ)
🔹 Q1. Does Go support native string interpolation?
A: No. Go does not support native interpolation like "Hello, ${name}". Use fmt.Sprintf() instead.
🔹 Q2. What is the difference between Sprintf and Printf?
A: Sprintf returns the formatted string; Printf prints it directly to the standard output.
🔹 Q3. Can I use template literals like in JavaScript?
A: No. Go doesn’t support template literals. You must use format strings and fmt.Sprintf().
🔹 Q4. How do I insert variables in a string in Go?
A: Use format specifiers like %s, %d, %f with fmt.Sprintf() to insert string, integer, or float variables.
🔹 Q5. Is string concatenation better than interpolation?
A: For simple strings, + can be faster. For complex or structured formatting, fmt.Sprintf() is preferred for readability and maintainability.
Share Now :
