Building Desktop Applications with Wails: A Comprehensive GuideThe Wails Project | Wails+7Welcome to Wails+7Coding Explorations+7
Image Source: lmmid.com
Introduction
Wails is a powerful framework that enables developers to build cross-platform desktop applications using Go for the backend and modern web technologies for the frontend. By combining the performance of Go with the flexibility of web frameworks like React, Vue, or Svelte, Wails offers a seamless development experience for creating native desktop apps.The Wails Project | Wails+10golang.ch+10GitHub+10Welcome to Wails
In this tutorial, we’ll walk through the process of setting up a Wails project, building a simple application, and understanding the core concepts that make Wails an excellent choice for desktop application development.
Prerequisites
Before we begin, ensure you have the following installed on your system:
- Go: Version 1.21 or higher. Download from the official Go website.
- Node.js and npm: Node.js version 15 or higher. Download from the official Node.js website.The Wails Project | Wails
To verify the installations:
bash复制编辑go version
npm --version
Additionally, Wails requires platform-specific dependencies. For instance, on Windows, the WebView2 runtime is necessary. You can check your system’s readiness using the wails doctor
command after installing Wails.The Wails Project | Wails+5The Wails Project | Wails+5The Wails Project | Wails+5
Installing Wails
To install the Wails CLI, run the following command:
bash
复制编辑
go install github.com/wailsapp/wails/v2/cmd/wails@latest
After installation, verify that Wails is correctly set up:
bash
复制编辑
wails doctor
This command checks for any missing dependencies and provides guidance on resolving them.
Creating a New Wails Project
Wails supports various frontend frameworks. For this tutorial, we’ll use the default vanilla JavaScript template.The Wails Project | Wails+2The Wails Project | Wails+2Coding Explorations+2
bash
复制编辑
wails init -n myapp
This command initializes a new Wails project named myapp
. The project structure will look like this:The Wails Project | Wails
go复制编辑myapp/
├── build/
├── frontend/
├── app.go
├── go.mod
├── main.go
└── wails.json
- build/: Contains the build output and platform-specific resources.
- frontend/: Houses the frontend source code.
- app.go: Defines the application logic and methods exposed to the frontend.
- main.go: Configures and runs the application.
- wails.json: Project configuration file.The Wails Project | WailsThe Wails Project | Wails+6golang.ch+6Coding Explorations+6
Understanding the Project Structure
app.go
This file contains the core application logic. Here’s a basic example:
go复制编辑package main
import (
"context"
"fmt"
)
type App struct {
ctx context.Context
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) shutdown(ctx context.Context) {
// Perform cleanup tasks here
}
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
The Greet
method can be invoked from the frontend, demonstrating the seamless communication between Go and JavaScript.GitHub
main.go
This file sets up and runs the application:Coding Explorations+1The Wails Project | Wails+1
go复制编辑package main
import (
"log"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
)
func main() {
app := NewApp()
err := wails.Run(&options.App{
Title: "MyApp",
Width: 800,
Height: 600,
OnStartup: app.startup,
OnShutdown: app.shutdown,
Bind: []interface{}{
app,
},
})
if err != nil {
log.Fatal(err)
}
}
The Bind
option exposes the methods defined in App
to the frontend.The Wails Project | Wails+1Coding Explorations+1
Running the Application
To start the application in development mode:
bash复制编辑cd myapp
wails dev
This command launches the application with hot-reloading enabled, allowing you to see changes in real-time.
To build the application for production:
bash
复制编辑
wails build
The compiled application will be located in the build/bin
directory.The Wails Project | Wails
Frontend Integration
Wails allows you to use your preferred frontend framework. For instance, to create a project with React and TypeScript:Coding Explorations
bash
复制编辑
wails init -n myreactapp -t react-ts
This command sets up a Wails project with a React and TypeScript frontend. You can then develop your UI components as you would in a typical React application.
Calling Go Methods from the Frontend
In the frontend, you can call Go methods using the @wailsapp/runtime
package. Here’s an example in a React component:Coding Explorations+1GitHub+1
tsx复制编辑import React, { useState } from 'react';
import { invoke } from '@wailsapp/runtime';
function App() {
const [name, setName] = useState('');
const [greeting, setGreeting] = useState('');
const greet = async () => {
const result = await invoke('Greet', { name });
setGreeting(result);
};
return (
<div>
<input value={name} onChange={e => setName(e.target.value)} />
<button onClick={greet}>Greet</button>
<p>{greeting}</p>
</div>
);
}
export default App;
This component captures user input and sends it to the Go backend, displaying the response.
Application Lifecycle Hooks
Wails provides lifecycle hooks to manage application startup and shutdown processes. These are defined in the App
struct:The Wails Project | Wails
go复制编辑func (a *App) startup(ctx context.Context) {
// Initialize resources
}
func (a *App) shutdown(ctx context.Context) {
// Clean up resources
}
These hooks are useful for setting up or tearing down resources as needed.The Wails Project | Wails+2The Wails Project | Wails+2Coding Explorations+2
Packaging the Application
After building the application using wails build
, you can distribute the executable found in the build/bin
directory. Wails supports packaging for Windows, macOS, and Linux, allowing you to share your application with users across different platforms.The Wails Project | Wails+2The Wails Project | Wails+2Welcome to Wails+2
Conclusion
Wails simplifies the process of building cross-platform desktop applications by combining the robustness of Go with the versatility of modern web technologies. Whether you’re developing a simple utility or a complex application, Wails provides the tools and structure needed to create efficient and native-feeling desktop apps.Subroto’s Portfolio+4Welcome to Wails+4Coding Explorations+4
Useful Resources
- Wails Official Documentation
- Wails GitHub Repository
- Wails Community Templates
Friendly Links
Note: This tutorial is inspired by the Jada 1:24 Diecast Mystery Machine with Scooby Green, showcasing the blend of creativity and technology.