只是提供一個 Web Server 的話,我們其實還可以搭配 Webview ,直接讓它變成一個 Local App:
package main
import (
_ "embed"
"github.com/mudream4869/toolgui/toolgui/tgcomp"
"github.com/mudream4869/toolgui/toolgui/tgexec"
"github.com/mudream4869/toolgui/toolgui/tgframe"
webview "github.com/webview/webview_go"
)
func main() {
app := tgframe.NewApp()
app.AddPage("index", "Index", func(p *tgframe.Params) error {
tgcomp.Text(p.Main, "Hello world")
return nil
})
e := tgexec.NewWebExecutor(app)
go e.StartService(":3000")
w := webview.New(false)
defer w.Destroy()
w.SetSize(1280, 640, webview.HintNone)
w.Navigate("http://localhost:3000/")
w.Run()
}
我在 WebExecutor 的介面也提供了取得 Mux 函數,如此一來,我們可以先 listen 0 port,由系統指派一個沒有用過的 port,避免撞 port。
package main
import (
_ "embed"
"fmt"
"log"
"net"
"net/http"
"github.com/mudream4869/toolgui/toolgui/tgcomp"
"github.com/mudream4869/toolgui/toolgui/tgexec"
"github.com/mudream4869/toolgui/toolgui/tgframe"
webview "github.com/webview/webview_go"
)
func main() {
app := tgframe.NewApp()
app.AddPage("index", "Index", func(p *tgframe.Params) error {
tgcomp.Text(p.Main, "Hello world")
return nil
})
e := tgexec.NewWebExecutor(app)
mux, err := e.Mux()
if err != nil {
log.Fatal(err)
return
}
listner, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
return
}
addr := listner.Addr().String()
go http.Serve(listner, mux)
w := webview.New(false)
defer w.Destroy()
w.SetSize(1280, 640, webview.HintNone)
w.Navigate(fmt.Sprintf("http://%s/", addr))
w.Run()
}