相关疑难解决方法(0)

用相同的变量替换Sprintf中的所有变量

是否可以使用fmt.Sprintf()相同的值替换格式化字符串中的所有变量?

就像是:

val := "foo"
s := fmt.Sprintf("%v in %v is %v", val)
Run Code Online (Sandbox Code Playgroud)

哪会回来

"foo in foo is foo"
Run Code Online (Sandbox Code Playgroud)

string format printf go

13
推荐指数
1
解决办法
2279
查看次数

在 Go 中,是否有类似 Python 中的“f-string”这样的特性?

在 Go 中,是否有类似 Python 中的“f-string”的特性?我找不到像 f-string 这样的简单解决方案。

#Python
name = 'AlphaGo'
print(f'I am {name}') ##I am AlphaGo
Run Code Online (Sandbox Code Playgroud)

我在网上和评论中找到的最佳替代解决方案是

//Golang
package main

import (
    "fmt"
)

func main() {
    const name, age = "Kim", 22
    fmt.Println(name, "is", age, "years old.") // Kim is 22 years old.
}

Run Code Online (Sandbox Code Playgroud)

但是,这仍然不像f-string那么简单......

go

10
推荐指数
1
解决办法
3523
查看次数

标签 统计

go ×2

format ×1

printf ×1

string ×1