Contents
22. Sprintf(格式化字符串)¶
您可能已经看到了诸如fmt.Println(“ some string”)之类的东西及其周围的变化。但是有时候您可能只想使用在fmt下找到的格式化工具来生成字符串,而不必在stdout上使用。那就是为什么使用fmt.Sprintf的目的。
22.1. Python¶
max = 10
try:
raise Exception("The max. number is {}".format(max))
except Exception as e:
print(e)
22.2. Go¶
package main
import "fmt"
func main() {
max := 10
defer func() {
fmt.Println("error was:", recover()) //error was: The max ,number is 10
}()
panic(fmt.Sprintf("The max ,number is %d", max))
}