英文:
In go 1.20.1 , is fmt.Printf("Processes took #{elapsed}") wrong?
问题
我正在学习
在go 1.20.1中,我的程序有问题吗?
文件 main.go
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
doSomething()
doSomethingElse()
fmt.Println("\n\n我猜我完成了")
elapsed := time.Since(start)
fmt.Printf("处理花费了 %s", elapsed)
}
func doSomething() {
time.Sleep(time.Second * 2)
fmt.Println("\n我做了一些事情")
}
func doSomethingElse() {
fmt.Println("做其他事情")
}
英文:
I am learning
In go 1.20.1, Is my program wrong
File main.go
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
doSomething()
doSomethingElse()
fmt.Println("\n\nI guess I'm done")
elapsed := time.Since(start)
fmt.Printf("Processes took #{elapsed}")
}
func doSomething() {
time.Sleep(time.Second * 2)
fmt.Println("\nI've done something")
}
func doSomethingElse() {
fmt.Println("do abc esle")
}
答案1
得分: 1
这是fmt
格式字符串的错误语法:
fmt.Printf("Processes took #{elapsed}")
正确的版本应该是:
fmt.Printf("Processes took %s", elapsed)
无论你使用哪个Go版本,都是如此。你可能看到第一个版本的原因是,GoLand会将第二个版本显示为第一个版本。视觉上的提示是,格式字符串是灰色的,而不是典型的绿色字符串文字,并且还有高亮显示。这是GoLand用来指示你所看到的不是实际代码,而是一个被翻译成更易读的版本的视觉格式。
第一个版本从来都不是有效的Go代码。它只是GoLand自动显示的一种简写形式。
英文:
This is incorrect syntax for fmt
format strings:
fmt.Printf("Processes took #{elapsed}")
The correct version would look like:
fmt.Printf("Processes took %s", elapsed)
This is regardless of which Go version you are using. The reason why you may have seen the first version is that GoLand will display the second version as if it were the first. The visual cue for this is that the format string is in grey, rather than the typical green for string literals, and also highlighted. That is the visual formatting that GoLand uses to indicate that what you're seeing is not the actual code, but a translated version that is supposed to be more readable.
The first version is not and was never valid Go code. It is just a shorthand that GoLand displays automatically.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论