3 回答

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用 暫停程序任意長(zhǎng)的時(shí)間time.Sleep()。例如:
package main
import ( "fmt"
"time"
)
func main() {
fmt.Println("Hello world!")
duration := time.Second
time.Sleep(duration)
}
要任意增加持續(xù)時(shí)間,您可以執(zhí)行以下操作:
duration := time.Duration(10)*time.Second // Pause for 10 seconds
編輯:由于 OP 為問(wèn)題添加了額外的約束,因此上面的答案不再符合要求。您可以Enter通過(guò)創(chuàng)建一個(gè)等待讀取換行符 ( \n) 字符的新緩沖區(qū)讀取器來(lái)暫停,直到按下該鍵。
package main
import ( "fmt"
"bufio"
"os"
)
func main() {
fmt.Println("Hello world!")
fmt.Print("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
package main
import "fmt"
func main() {
fmt.Println("Press the Enter Key to terminate the console screen!")
fmt.Scanln() // wait for Enter Key
}

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
最少導(dǎo)入的最簡(jiǎn)單的另一種方法使用這 2 行:
var input string
fmt.Scanln(&input)
在程序末尾添加這一行,將暫停屏幕直到用戶按下 Enter 鍵,例如:
package main
import "fmt"
func main() {
fmt.Println("Press the Enter Key to terminate the console screen!")
var input string
fmt.Scanln(&input)
}
- 3 回答
- 0 關(guān)注
- 442 瀏覽
添加回答
舉報(bào)