英文:
Golang equivalent to Python Formatted String using F-strings
问题
在Python中,可以使用f-string来格式化字符串,如下所示:
name1 = 'Ele'
name2 = 'Ben'
name3 = 'Frank'
age = 45
print(f"My name is {name1} but I also get called {name2} and at times {name3}. Generally, I prefer {name1} and my age is {age}")
你想知道Golang中是否有类似的功能,可以在字符串中指定变量的位置。目前,Golang中没有直接等价的功能,但可以使用类似的方式实现,如下所示:
name1 := "Ele"
name2 := "Ben"
name3 := "Frank"
age := 45
message := fmt.Sprintf("My name is %s but I also get called %s and at times %s. Generally, I prefer %s and my age is %d", name1, name2, name3, name1, age)
fmt.Println(message)
在Golang中,你需要使用fmt.Sprintf
函数来格式化字符串,并在字符串中使用占位符(如%s
和%d
)来指定变量的位置。然后,你需要按照相同的顺序提供变量的值。虽然这种方式可能会导致变量的重复,但目前Golang没有直接支持指定变量位置的功能。
英文:
In python one can use f-string to format strings like this
name1 = 'Ele'
name2 = 'Ben'
name3 = 'Frank'
age = 45
print(f"My name is {name1} but i also get called {name2} and at times {name3}. Generally I prefer {name1} and my age is {age}")
what is golang's equivalent to this? where i can specify exactly which variable at what spot
currently this is all i see with golang, but it creates repeating variables unnecessarily like below
name1 := "Ele"
name2 := "Ben"
name3 := "Frank"
age := 45
message := fmt.Sprintf("My name is %s but i also get called %s and at times %s. Generally I prefer %s and my age is %d", name1, name2, name3, name1, age)
fmt.Println(message)
Imagine if i need to repeat a variable multiple times in the same string, i will have to keep repeating it and then need to monitor the position of the variable always to make sure they align correctly
Is there a way similar to f-string in python for golang?
答案1
得分: 1
你可以使用text/template
。
对于这个简单的示例来说,可能有点过度,但是在处理复杂模板、大量输出等情况下,它可能是值得的。
package main
import (
"bytes"
"fmt"
"log"
"text/template"
)
func main() {
data := fdata{
"Name1": "Ele",
"Name2": "Ben",
"Name3": "Frank",
"Age": 45,
}
format := "My name is {{.Name1}} but I also get called {{.Name2}} and at times {{.Name3}}. Generally I prefer {{.Name1}} and my age is {{.Age}}."
result, err := fstring(format, data)
if err != nil {
log.Fatalf("fstring() failed: %v", err)
}
fmt.Println(result)
}
type fdata map[string]interface{}
func fstring(format string, data fdata) (string, error) {
t, err := template.New("fstring").Parse(format)
if err != nil {
return "", fmt.Errorf("error creating template: %v", err)
}
output := new(bytes.Buffer)
if err := t.Execute(output, data); err != nil {
return "", fmt.Errorf("error executing template: %v", err)
}
return output.String(), nil
}
输出:
My name is Ele but I also get called Ben and at times Frank. Generally I prefer Ele and my age is 45.
英文:
You can use text/template
.
It's a bit overkill for this simple example, but it can be worth the overhead when dealing with complex templates, lots of output, etc.
package main
import (
"bytes"
"fmt"
"log"
"text/template"
)
func main() {
data := fdata{
"Name1": "Ele",
"Name2": "Ben",
"Name3": "Frank",
"Age": 45,
}
format := "My name is {{.Name1}} but I also get called {{.Name2}} and at times {{.Name3}}. Generally I prefer {{.Name1}} and my age is {{.Age}}."
result, err := fstring(format, data)
if err != nil {
log.Fatalf("fstring() failed: %v", err)
}
fmt.Println(result)
}
type fdata map[string]interface{}
func fstring(format string, data fdata) (string, error) {
t, err := template.New("fstring").Parse(format)
if err != nil {
return "", fmt.Errorf("error creating template: %v", err)
}
output := new(bytes.Buffer)
if err := t.Execute(output, data); err != nil {
return "", fmt.Errorf("error executing template: %v", err)
}
return output.String(), nil
}
Output:
My name is Ele but I also get called Ben and at times Frank. Generally I prefer Ele and my age is 45.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论