Walk in a cross-platform application

Walk is clearly a library aimed at creating graphical user interfaces for the Microsoft Windows platform—but this doesn't mean that building your application with Walk limits you to Windows only. Using the techniques explored in Chapter 3Go to the Rescue!, we can set the code for Windows to be conditionally included when building for the platform, and introduce other files that could provide a user interface for other platforms.

The first step is to update the files we have built so far to only build on Windows. We do this using the build constraints comment format (you could also use file naming for this step if you wish):

// +build windows

package main

...

We then introduce a new file that will handle the fallback case when we're on a different platform. For this simple project we will call it nonwindows.go as the content will run for any computer not running Windows. In this file, we place a small amount of code that will print a failure message and quit if the application is launched on any unsupported platform. Note that the build constraint here is set to compile on any non-Windows platform; this too would be updated to match any fallback cases your project may have:

// +build !windows

package main

import "log"

func NewMailUIBrowse() {
log.Fatalln("GoMail with Walk only works on windows")
}

Note the NewMailUIBrowse() function name—this is our generic method name for loading and running the main GoMail browse interface. You probably need to update the name of the method that was previously used to run the application. Most likely, you used main(), but we will need to provide a new main.go with that method. This new file is the only file in the project with no build constraints. It will compile for any platform and, when running, it will execute whichever NewMailUIBrowse() method was compiled in for the target platform:

package main

func main() {
NewMailUIBrowse()
}

If we switch to another operating system, say macOS, and compile the code now, there should be no compile errors. Running the application will yield a simple error message and it will immediately quit. Clearly this code could do something more meaningful than just exiting with an error message:

And so you see how we can use Walk to develop a Windows-specific user interface. As part of a multi-platform strategy, this could help ensure greater platform integration for your audience on Windows, or you may wish to provide certain sections of your application with platform-specific implementations. Whatever the reason, you can see how easy it is to include multiple platform-specific alternatives within a cross-platform application build with Go.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.227.4