Basics
String Interpolation
How JavaScript's template literals (`${}`) compare to Go's fmt.Sprintf, Rust's format!/println!, Swift's string interpolation, and Java's String.formatted.
- Minimum versions
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 2.0Java ≥ 25
- Verified on
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.
JavaScript has string interpolation built right into the language via template literals (backtick strings). Go has no dedicated syntax for this — you use fmt.Sprintf with format verbs (%s, %d, …), similar to C's printf. Swift has real string interpolation built into the language: \(...) accepts any expression. Rust is different: format!/println! are just standard-library macros, not core language syntax; since Rust 1.58 they let you capture a plain identifier already in scope directly inside the braces ({name}), but they don't accept expressions or field access there — {user.name} or {a + b} simply won't compile, so you have to bind a local variable first or use positional/named arguments (format!("{}", user.name)). Java has no string interpolation at all — even the String Templates feature that was previewed in Java 21/22 was withdrawn — so it's still String.format/.formatted() in the printf style, just like Go.
String interpolation
const name = 'bob'
const age = 21
const message = `${name} is ${age} years old`
console.log(message)package main
import "fmt"
func main() {
name := "bob"
age := 21
message := fmt.Sprintf("%s is %d years old", name, age)
fmt.Println(message)
}fn main() {
let name = "bob";
let age = 21;
let message = format!("{name} is {age} years old"); // interpolates the identifier directly
println!("{message}");
}let name = "bob"
let age = 21
let message = "\(name) is \(age) years old"
print(message)void main() {
String name = "bob";
int age = 21;
String message = "%s is %d years old".formatted(name, age); // no string interpolation
IO.println(message);
}bob is 21 years oldReference: github.com/miguelmota/golang-for-nodejs-developers#interpolation