String Interpolation — Template Strings for Go

GALA replaces fmt.Sprintf with string interpolation. Write s"Hello $name" instead of fmt.Sprintf("Hello %s", name). Format verbs are inferred automatically from the expression type. No import "fmt" needed — the transpiler handles it.

Two prefixes are available:

val name = "Alice"
val age = 30
Println(s"$name is $age years old")  // Alice is 30 years old
Println(f"$age%04d")                 // 0030

s-strings: Auto-Inferred Format Verbs

Use $variable to embed a variable and ${expression} to embed any expression. The compiler picks the correct format verb from the expression’s type:

val name = "Alice"
val age = 30
val pi = 3.14159

Println(s"Hello $name!")                    // Hello Alice!
Println(s"$name is $age years old")         // Alice is 30 years old
Println(s"Pi ≈ $pi")                        // Pi ≈ 3.14159
Println(s"Next year: ${age + 1}")           // Next year: 31
Println(s"Price: $$99")                     // Price: $99 (use $$ for literal $)

Auto-Inferred Format Verb Table

The compiler selects the format verb based on the expression type:

Type Format Verb Example
string %s s"Hello $name"
int, int64, etc. %d s"Count: $n"
float64, float32 %g s"Pi: $pi"
bool %t s"Active: $flag"
rune %c s"Char: $ch"
everything else %v s"Data: $obj"

Expression Interpolation with ${...}

Any expression can be embedded inside ${...}, including function calls, method chains, arithmetic, and even expressions containing string literals:

val x = 10
val y = 20

Println(s"Sum: ${x + y}")                          // Sum: 30
Println(s"Upper: ${strings.ToUpper("hello")}")      // Upper: HELLO

Nested Strings Inside ${...}

The lexer correctly handles string literals inside interpolation expressions — you can pass string arguments to functions without escaping:

import "strings"
import . "martianoff/gala/collection_immutable"

// Function call with string argument
Println(s"result: ${strings.Repeat("ab", 3)}")         // result: ababab

// Multiple string arguments
Println(s"${strings.Replace("hello world", "world", "gala", 1)}")  // hello gala

// Collection methods with string separators
val nums = ArrayOf(1, 2, 3)
Println(s"nums: ${nums.MkString(", ")}")                // nums: 1, 2, 3

f-strings: Explicit Format Specifiers

When you need precise formatting — padding, decimal places, hex output — use f"..." with a Go printf format specifier after the variable or expression:

val count = 7
val price = 19.99

Println(f"$count%04d items")                // 0007 items
Println(f"Total: $$$price%.2f")             // Total: $19.99
Println(f"Hex: $count%x")                   // Hex: 7
Println(f"${count * 100}%010d")             // 0000000700

If you omit the %spec after a variable in an f"..." string, the format verb is auto-inferred (same behavior as s"...").

Common Format Specifiers

Specifier Meaning Example
%d Decimal integer f"$n%d"
%04d Zero-padded to 4 digits f"$n%04d"
%x Hexadecimal f"$n%x"
%f Floating point f"$x%f"
%.2f 2 decimal places f"$price%.2f"
%010d Zero-padded to 10 digits f"$n%010d"
%s String f"$name%s"

Println and Print Without Imports

Println and Print are available globally in GALA — no import "fmt" required. They map directly to fmt.Println and fmt.Print in the generated Go code:

// No import needed
Println("Hello, World!")
Println(s"Result: $x")
Print("no newline")

You only need to import "fmt" for specialized functions like fmt.Errorf, fmt.Fprintf, or fmt.Stringer.


Comparison: GALA Interpolation vs Go fmt.Sprintf

GALA

```gala val name = "Alice" val age = 30 val balance = 1234.5 Println(s"Hello $name!") Println(s"$name is $age years old") Println(f"Balance: $$$balance%.2f") Println(s"Next year: ${age + 1}") ```

Go

```go name := "Alice" age := 30 balance := 1234.5 fmt.Printf("Hello %s!\n", name) fmt.Printf("%s is %d years old\n", name, age) fmt.Printf("Balance: $%.2f\n", balance) fmt.Printf("Next year: %d\n", age+1) ```

Key differences:


When to Use s-strings vs f-strings

Need Use
Simple variable embedding s"Hello $name"
Expression embedding s"Total: ${x + y}"
Specific decimal places f"$price%.2f"
Zero padding f"$id%06d"
Hex / octal output f"$n%x"
No format control needed s"..." (simpler, auto-inferred)

Use s"..." by default. Switch to f"..." only when you need explicit format control.


Further Reading