```go
import (
"fmt"
"github.com/lxn/walk"
"github.com/lxn/walk/declarative"
)
type Windows interface {
ShowWindows()//展示窗体界面
}
//创建解压缩的界面类
type ComWindow struct{ // 窗口
Windows
*walk.MainWindow //主窗体
}
type LabWindow struct{ // 信息显示
Windows
}
func Show(windows_type string){
var Win Windows
switch windows_type {
case "main_window":
Win = new(ComWindow)
case "lab_window":
Win = &LabWindow{}
default:
fmt.Println("参数传递错误")
}
Win.ShowWindows()// 显示出来
}
func (comWindow *ComWindow) ShowWindows(){
pathWindow := new(ComWindow)
err:=declarative.MainWindow{
AssignTo:&pathWindow.MainWindow, //将ComWindow中的MainWindow的二级指针与declarative中的MainWindow相关联,关联主窗体
Title:"文件压缩", //窗口标题名称
MaxSize:declarative.Size{480,230}, //MaxSize是一个结构体类型
}.Create()//使用结构方法可以直接调用
if err!= nil{
fmt.Println(err)
}
//窗口的展示需要通过坐标来指定
pathWindow.SetX(650)
pathWindow.SetY(300)
pathWindow.Run() // 运行窗口,才能将创建的窗口展示出来
}
func main(){
Show()
}
```
下面是报错
```
Stack:
goroutine 1 [running, locked to thread]:
runtime/debug.Stack()
D:/Go/src/runtime/debug/stack.go:24 +0x65
github.com/lxn/walk.newErr(...)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/error.go:81
github.com/lxn/walk.newError({0x63c466, 0x12})
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/error.go:85 +0x27
github.com/lxn/walk.(*ToolTip).addTool(0x40aedd, 0x6907a8, 0x80)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/tooltip.go:184 +0xb6
github.com/lxn/walk.(*ToolTip).AddTool(0xc0000a0080, {0x6907a8, 0xc000047180})
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/tooltip.go:160 +0x37
github.com/lxn/walk.(*WidgetBase).init(0xc000047180, {0xe47788, 0xc000047180})
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/widget.go:137 +0xbe
github.com/lxn/walk.initWindowWithCfg(0xc000074bd0)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:670 +0x639
github.com/lxn/walk.InitWindow({0xe47788, 0xc000047180}, {0x68daf8, 0xc000009400}, {0x640f67, 0x6e}, 0x74c90, 0xc0)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:538 +0xa5
github.com/lxn/walk.InitWidget({0x6907a8, 0xc000047180}, {0x68daf8, 0xc000009400}, {0x640f67, 0x1c}, 0x50000000, 0x0)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/widget.go:110 +0xc5
github.com/lxn/walk.NewCompositeWithStyle({0x68daf8, 0xc000009400}, 0x0)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/composite.go:30 +0xc5
github.com/lxn/walk.NewComposite({0x690198, 0xc000009400})
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/composite.go:45 +0x37
github.com/lxn/walk.(*FormBase).init(0xc000009400, {0xe47400, 0xc000009400})
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/form.go:107 +0x38
github.com/lxn/walk.initWindowWithCfg(0xc000074f50)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:662 +0x5dd
github.com/lxn/walk.NewMainWindowWithCfg(0xc000075040)
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/mainwindow.go:48 +0x12b
github.com/lxn/walk/declarative.MainWindow.Create({{{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, ...}, ...})
C:/Users/Administrator/go/pkg/mod/github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/declarative/mainwindow.go:64 +0xfa
main.(*ComWindow).ShowWindows(0xc00000e480)
D:/Go/awesomeProject/src/windows.go:40 +0xac
main.Show({0x63966f, 0xc00002c000})
D:/Go/awesomeProject/src/windows.go:30 +0xdf
main.main()
D:/Go/awesomeProject/src/main.go:6 +0x25
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x5b4b1f]
goroutine 1 [running, locked to thread]:
main.(*ComWindow).ShowWindows(0xc00000e480)
D:/Go/awesomeProject/src/windows.go:45 +0xff
main.Show({0x63966f, 0xc00002c000})
D:/Go/awesomeProject/src/windows.go:30 +0xdf
main.main()
D:/Go/awesomeProject/src/main.go:6 +0x25
Process finished with exit code 2
```
有疑问加站长微信联系(非本文作者)