Skip to content

proposal for better string formatting #34523

Closed
@cyclingwithelephants

Description

@cyclingwithelephants

I really like python style f strings, as below. This drastically improves readability over using the older format method.

name = "Eric"
age = 74

# f strings
print( f"Hello, {name}. You are {age}.")
'Hello, Eric. You are 74.'

# older format method
print("Hello, %s. You are %d".format(name, age))
'Hello, Eric. You are 74.'

This has the obvious benefit of higher readability (A core design decision of Go) and also scales better as the strings that one deals with become longer and more complicated (another core design decision of Go). Please notice also that using {age} gives the string context that it otherwise wouldn't have if you were only skim reading the string (and of course ignoring the type that was specified), the string could have ended "You are tall", "You are [at XXX location]", "You are working too hard" and unless you put in the mental energy to map the format method to each instance of interpolation it isn't immediately obvious what should go there. By removing this (admittedly small) mental hurdle the programmer can focus on the logic rather than the code.

I was wondering if others shared my interest for a similar implementation in Go, of course with strict type formatting it couldn't look exactly the same but purely as a jumping off point is an implementation of roughly shape below possible?

name := "Eric"
age := 74
fmt.println("Hello %s{name}. You are %d{age}")
'Hello, Eric. You are 74.'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions