自学内容网 自学内容网

golang fmt.Sprintf 引用前述变量

可使用 %[1]s 之类的写法,引用前述变量,避免重复写
例:

package main

import "fmt"

func main() {
s := fmt.Sprintf("%[1]s, %[2]d, %[3]s, %[1]s %[3]s", "hello", 777, "world")
fmt.Print(s)  // 输出:hello, 777, world, hello world
}
  • 参考 https://go.dev/play/p/BC1fY-O60Af

原文地址:https://blog.csdn.net/xchenhao/article/details/142590707

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!