无法将GUI变量保持为全局

I find following code works:

// modified from: https://github.com/andlabs/ui/wiki/Getting-Started
package main
import ("github.com/andlabs/ui")
func makewinfn() {
    var name = ui.NewEntry()
    var button = ui.NewButton("Greet")
    var greeting = ui.NewLabel("")
    box := ui.NewVerticalBox()
    box.Append(ui.NewLabel("Enter your name:"), false)
    box.Append(name, false)
    box.Append(button, false)
    box.Append(greeting, false)
    mywindow := ui.NewWindow("MyTitle", 200, 100, false)
    mywindow.SetChild(box)
    button.OnClicked( func (*ui.Button) {greeting.SetText("Hello, " + name.Text() + "!") } )
    mywindow.OnClosing( func (*ui.Window) bool { ui.Quit(); return true } )
    mywindow.Show()
}
func main() {
    ui.Main(makewinfn)
}

However, if I try with global variables:

package main
import ("github.com/andlabs/ui")
// keeping following as global variables: 
var name = ui.NewEntry()
var button = ui.NewButton("Greet")
var greeting = ui.NewLabel("")
func makewinfn() {
    box := ui.NewVerticalBox()
    box.Append(ui.NewLabel("Enter your name:"), false)
    box.Append(name, false)
    box.Append(button, false)
    box.Append(greeting, false)
    mywindow := ui.NewWindow("MyTitle", 200, 100, false)
    mywindow.SetChild(box)
    button.OnClicked( func (*ui.Button) {greeting.SetText("Hello, " + name.Text() + "!") } )
    mywindow.OnClosing( func (*ui.Window) bool { ui.Quit(); return true } )
    mywindow.Show()
}
func main() {
    ui.Main(makewinfn)
}

This code with global variables compiles all right but creates following error on running:

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fecb2712e19]

How can I keep GUI components as global variables? I have keep them as global so that I can access them from other functions.

When you use variables at the top level (package block), they are initialized before execution of main() begins.

And you call code from the github.com/andlabs/ui package, but its ui.Main() was not yet called, and so the ui package and resources it depends on may not have been initialized.

Only declare the variables but do not yet assign values to them, leave that to the makewinfn() function:

var name *ui.Entry
var button *ui.Button
var greeting *ui.Label

func makewinfn() {
    name = ui.NewEntry()
    button = ui.NewButton("Greet")
    greeting = ui.NewLabel("")

    box := ui.NewVerticalBox()
    // ...
}