switch msg.(type) { case tea.KeyMsg: return m, tea.Quit case tickMsg: m -= 1 if m <= 0 { return m, tea.Quit } return m, tick } return m, nil }
func(m model) View() string { fmt.Println("1. action View func") return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m) }
// main函数 // 只需要调用NewProgram函数,调用其返回的变量方法Start即可完成 funcmain() { // Initialize our program p := tea.NewProgram(model(5)) if err := p.Start(); err != nil { log.Fatal(err) } }
运行代码
1 2 3 4 5 6 7 8 9
go build main.go main.exc
//输出 Hi. This program will exit in 5 seconds. To quit sooner press any key. Hi. This program will exit in 4 seconds. To quit sooner press any key. Hi. This program will exit in 3 seconds. To quit sooner press any key. Hi. This program will exit in 2 seconds. To quit sooner press any key. Hi. This program will exit in 1 seconds. To quit sooner press any key.