Go walk 使用經歷

📢 本文由 gemini-3.5-flash 翻譯

在 2024 年 12 月的時候我試著寫了人生的第一個 Windows GUI 軟體,別看螢幕了,Go walk (去散步)

載入中...
- -

雖然非常簡陋但也算是自己第一次沒怎麼使用框架開發的感覺,同時當時無論如何都想使用 Go 來著,正好看到了這麼符合主題的框架,同時以技術棧呼應主題的雙關

所以今天再次翻到這份筆記的時候感觸良多,尤其是這個軟體甚至支援 i18n,在如今 AI Coding 盛行的時候,很難想像當時我居然古法手搓的還支援多語言,主要是我印象中這個框架不原生支援來著,不過因為不好用,也忘記了

感想寫到這裡,下方是筆記原文

文章僅用於發布當時的筆記,不保證準確性,另外不推薦 walk 架構

環境

GoLand 新建一個 Go 專案後,預設會初始化 go.mod,若無此檔案,可以使用命令

1
go mod init

安裝 walk

1
go get github.com/tailscale/walk

專案

下載 rsrc 工具 並配置好 PATH 後,安裝 rsrc

1
go get github.com/akavel/rsrc

建立 Manifest 檔案 main.manifest,內容如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
        </dependentAssembly>
    </dependency>
    <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings>
            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
            <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
        </windowsSettings>
    </application>
</assembly>

合併到 Go 程式

1
rsrc -manifest main.manifest -o rsrc.syso

倒數計時開發

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main

import (
	"fmt"
	"github.com/tailscale/walk"
	. "github.com/tailscale/walk/declarative"
	"log"
	"runtime"
	"time"
)

func main() {
	app, err := walk.InitApp()
	if err != nil {
		log.Fatal(err)
	}

	var timeRemain *walk.Label
	var flag bool
	ok := true

	MainWindow{
		Title:   "Hi walk",                   // 視窗標題
		MinSize: Size{Width: 60, Height: 40}, // 最小尺寸
		Layout:  VBox{MarginsZero: true},     // 視窗佈局,垂直盒子佈局
		Children: []Widget{
			Label{
				AssignTo: &timeRemain,
				Text:     "開始計時 10 秒",
			},
			PushButton{
				Text: "開始計時",
				OnClicked: func() {
					if flag {
						return
					}
					flag = true
					// 計時
					go func() {
						for i := 10; i > 0; i-- {
							if ok {
								time.Sleep(1 * time.Second)
								timeRemain.SetText(fmt.Sprintf("倒數計時 %d 秒", i))
							} else {
								runtime.Goexit()
							}
						}
						walk.MsgBox(nil, "提醒", "時間到", walk.MsgBoxIconInformation)
						flag = false
					}()
				},
			},
			PushButton{
				Text: "停止計時",
				OnClicked: func() {
					flag = false
					ok = false
					timeRemain.SetText("計時已經停止")
					time.Sleep(1 * time.Second)
					timeRemain.SetText("開始計時 10 秒")
				},
			},
		},
	}.Create()

	app.Run()
}

新視窗

打開一個新視窗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
	"github.com/tailscale/walk"
	. "github.com/tailscale/walk/declarative"
	"log"
)

type MyMainWindow struct {
	*walk.MainWindow
}

func main() {
	app, err := walk.InitApp()
	if err != nil {
		log.Fatal(err)
	}

	mw := new(MyMainWindow)

	MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "Hi walk",                     // 視窗標題
		MinSize:  Size{Width: 400, Height: 500}, // 最小尺寸
		Size:     Size{Width: 400, Height: 500}, // 尺寸
		Layout:   VBox{},                        // 視窗佈局
		Children: []Widget{
			Label{
				Text:      "請選擇模式",
				Alignment: AlignHCenterVCenter,
			},
			PushButton{
				Text:      "短時間高頻休息",
				OnClicked: mw.shortLayout,
			},
			PushButton{
				Text: "長時間低頻休息",
			},
		},
	}.Create()

	app.Run()

}

func (mw *MyMainWindow) shortLayout() {
	shortMainWindow := new(MyMainWindow)
	MainWindow{
		AssignTo: &shortMainWindow.MainWindow,
		Title:    "短時間高頻休息",
		MinSize:  Size{Width: 400, Height: 500}, // 最小尺寸
		Size:     Size{Width: 400, Height: 500}, // 尺寸
		Layout:   VBox{},
		Children: []Widget{
			Label{Text: "Hello"},
		},
	}.Create()

	shortMainWindow.Running()
}

Reference

go walk 兩天的使用情況,心中默默放棄

Golang—walk學習(一)

walk 控制項學習第一節

This post is licensed under CC BY-NC-SA 4.0 by the author.

訪問統計

自 2025-02-08 起的訪問統計

使用 Hugo 建立 | 主題 StackJimmy 設計 | 修改自 yexca