Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal for better string formatting #34523

Closed
cyclingwithelephants opened this issue Sep 25, 2019 · 2 comments
Closed

proposal for better string formatting #34523

cyclingwithelephants opened this issue Sep 25, 2019 · 2 comments

Comments

@cyclingwithelephants
Copy link

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.'
@mvdan
Copy link
Member

mvdan commented Sep 25, 2019

Duplicate of #34174? There's probably no point in having multiple, similar proposals open at the same time.

@bradfitz
Copy link
Contributor

Yes, closing as a dup.

@golang golang locked and limited conversation to collaborators Sep 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants