注意三点:

1、交叉编译,要把我的 go 代码编译为 window 执行的文件
2、编译 html 等静态文件到 exe 文件中
3、启动 window 的浏览器

package main
import (
    _ "embed"
    "log"
    "net"
    "net/http"

    "github.com/gin-gonic/gin"
    "github.com/skratchdot/open-golang/open"
)
// 这里不能有空格
//go:embed templates/transaction.html
var content []byte

func main() {
    l, err := net.Listen("tcp", "localhost:3000")
    if err != nil {
        log.Fatal(err)
    }
    r := SetRouter()

    // 使用第三方的包打开chrome
    open.RunWith("http://localhost:3000/transaction", "chrome")
    // Start the blocking server loop
    http.Serve(l, r)
}

func SetRouter() *gin.Engine {
    r := gin.Default()
    // 显示发起交易页面
    r.GET("/transaction", func(c *gin.Context) {
        c.Data(http.StatusOK, "text/html", content)
    })
    return r
}

交叉编译

Linux 下编译 Mac 和 Windows 64 位可执行程序

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Mac 下编译 Linux 和 Windows 64 位可执行程序

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Windows 下编译,依次执行如下命令

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build -o goblog

通过如下命令可查看 Go 支持 OS 和平台列表:

go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
freebsd/arm64
illumos/amd64
ios/amd64
ios/arm64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
.
.
.
windows/386
windows/amd64
windows/arm
windows/arm64

参考编辑:https://learnku.com/articles/70273?#reply275739