1

I'm trying to follow the Wails manual and wanted to learn about Menus (V2.11).

The snippet from the example leads to an error:

undefined rt

What do I have to change to make this snippet run?

There is a hint to the meaning of the variable rt in the comment:

 FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) { // `rt` is an alias of "github.com/wailsapp/wails/v2/pkg/runtime" to prevent collision with standard package rt.Quit(app.ctx) }) 
New contributor
milvus1962 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Not sure why this was closed, as it contains enough information to answer, and I answered it with a running example. It shows the error, has desired behavior, and has a code snippet that reproduces it. IMO, the snippet could use a bit more surrounding code, but even the full docs snippet reproduces this error (and others). On the other hand, one could also say it has more code than necessary to reproduce currently as a one-liner would suffice. If anything, I'd close as duplicate of the import alias question I linked, but IMO this is a specific instance of that which is independently useful Commented 2 days ago

1 Answer 1

0

Import Alias

 // `rt` is an alias of "github.com/wailsapp/wails/v2/pkg/runtime" to prevent collision with standard package 

The comment is referring to an import alias. An import alias is a local renaming of the imported package to be used in the file. See also the question How to import and use different packages of the same name.

In this case, the comment means that instead of using the name runtime, they have aliased it to rt. For example:

import ( rt "github.com/wailsapp/wails/v2/pkg/runtime" // aliased as `rt` ) 

This is necessary because the example code you are following also uses the standard library's runtime package, and so has to differentiate the two runtime packages to avoid a naming collision. For instance:

import ( "runtime" // `runtime` rt "github.com/wailsapp/wails/v2/pkg/runtime" // aliased as `rt` ) 

Now you can use both packages together in the same file without a compilation error.

Complete Example

We can add imports to the menu example for a complete, runnable example as such:

menu.go:

package main import ( "log" "runtime" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/menu" "github.com/wailsapp/wails/v2/pkg/menu/keys" "github.com/wailsapp/wails/v2/pkg/options" rt "github.com/wailsapp/wails/v2/pkg/runtime" ) func main() { // Sourced largely from (with some fixes): https://github.com/wailsapp/wails/blob/4c464b3092fff35b993f31ca62ca612399dde970/website/docs/reference/menus.mdx?plain=1#L13 app := NewApp() AppMenu := menu.NewMenu() if runtime.GOOS == "darwin" { AppMenu.Append(menu.AppMenu()) // On macOS platform, this must be done right after `NewMenu()` } FileMenu := AppMenu.AddSubmenu("File") FileMenu.AddText("&Open", keys.CmdOrCtrl("o"), func(_ *menu.CallbackData) { // do something }) FileMenu.AddSeparator() FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) { // `rt` is an alias of "github.com/wailsapp/wails/v2/pkg/runtime" to prevent collision with standard package rt.Quit(app.ctx) }) if runtime.GOOS == "darwin" { AppMenu.Append(menu.EditMenu()) // On macOS platform, EditMenu should be appended to enable Cmd+C, Cmd+V, Cmd+Z... shortcuts } err := wails.Run(&options.App{ Title: "Menus Demo", Width: 800, Height: 600, Menu: AppMenu, // reference the menu above Bind: []interface{}{ app, }, }) // ... if err != nil { log.Fatal(err) } } 

To get this to fully run, we also need to include the NewApp() / app.go scaffold in another file:

app.go (source example):

package main import ( "context" "fmt" ) // App struct type App struct { ctx context.Context } // NewApp creates a new App application struct func NewApp() *App { return &App{} } // startup is called when the app starts. The context is saved // so we can call the runtime methods func (a *App) startup(ctx context.Context) { a.ctx = ctx } // Greet returns a greeting for the given name func (a *App) Greet(name string) string { return fmt.Sprintf("Hello %s, It's show time!", name) } 

With these two files fully defined, you can now compile this on the Go Playground. Note that it eventually errors out as it can't be fully run in that environment.

Sign up to request clarification or add additional context in comments.

1 Comment

I wrote up a PR to the documentation to make the imports more explicit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.