在go 1.20.1中,fmt.Printf(“Processes took #{elapsed}”)是错误的吗?

huangapple go评论82阅读模式
英文:

In go 1.20.1 , is fmt.Printf("Processes took #{elapsed}") wrong?

问题

我正在学习

在go 1.20.1中,fmt.Printf(“Processes took #{elapsed}”)是错误的吗?

相关链接 https://github.com/donhuvy/Up-and-Running-with-Concurrency-in-Go-Golang-/blob/main/4-First_Goroutine/NotUsingGoroutines.go#L20

在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("做其他事情")
}

在go 1.20.1中,fmt.Printf(“Processes took #{elapsed}”)是错误的吗?

英文:

I am learning

在go 1.20.1中,fmt.Printf(“Processes took #{elapsed}”)是错误的吗?

related https://github.com/donhuvy/Up-and-Running-with-Concurrency-in-Go-Golang-/blob/main/4-First_Goroutine/NotUsingGoroutines.go#L20

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")
}

在go 1.20.1中,fmt.Printf(“Processes took #{elapsed}”)是错误的吗?

答案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.

huangapple
  • 本文由 发表于 2023年2月21日 11:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75516027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定