Bindings
Introduction
One of the key features of Wails is the ability to seamlessly integrate backend Go code with the frontend, enabling efficient communication between the two. This can be done manually by sending messages between the frontend and backend, but this can be cumbersome and error-prone, especially when dealing with complex data types.
The bindings generator in Wails v3 simplifies this process by automatically generating JavaScript or TypeScript functions and models that reflect the methods and data structures defined in your Go code. This means you can write your backend logic in Go and easily expose it to the frontend without the need for manual binding or complex integration.
This guide is designed to help you understand and utilize this powerful binding tool.
Core Concepts
In Wails v3, services can be added to your application. These services act as a bridge between the backend and frontend, allowing you to define methods and state that can be accessed and manipulated from the frontend.
Services
- Services can hold state and expose methods that operate on that state.
- Services can be used similar to controllers in HTTP web applications or as services.
- Only public methods on the service are bound, following Go’s convention.
Here’s a simple example of how you can define a service and add it to your Wails application:
In this example, we define a GreetService
services with a public Greet
method. The Greet
method takes a name
parameter and returns a greeting
string.
We then create a new Wails application using application.New
and add the
GreetService
service to the application using the Services
option in the
application.Options
. The application.NewService
method must always be given
an instance of the service struct, not the service struct type itself.
Generating the Bindings
By binding the struct, Wails is able to generate the necessary JavaScript or TypeScript code by running the following command in the project directory:
The bindings generator will scan the project and dependencies for anything that needs generating. Note: It will take longer the very first time you run the bindings generator, as it will be building up a cache of packages to scan. You should see output similar to the following:
If we look in the frontend/bindings
directory, we should see the following
files:
Directoryfrontend/bindings
Directorychangeme
- greetservice.js
- index.js
NOTE: The changeme
directory is the name of the module defined in go.mod
and
is used to namespace the generated files.
The generated greetservice.js
file contains the JavaScript code that mirrors
the Go struct and its methods:
As you can see, it also generates all the necessary JSDoc type information to ensure type safety in your frontend code.
Using the Bindings
You can import and use this file in your frontend code to interact with the backend.
Binding Models
In addition to binding methods, you can also use structs as input or output parameters in your bound methods. When structs are used as parameters, Wails generates corresponding JavaScript versions of those types.
Let’s extend the previous example to use a Person
type that has a Name
field:
In this updated example, we define a Person
struct with a Name
field. The
Greet
method in the GreetService
service now takes a Person
as an input
parameter.
When you run the bindings generator, Wails will generate a corresponding
JavaScript Person
type that mirrors the Go struct. This allows you to create
instances of the Person
type in your frontend code and pass them to the bound
Greet
method.
If we run the bindings generator again, we should see the following output:
In the frontend/bindings/changeme
directory, you should see a new models.js
file containing the following code:
The Person
class is generated with a constructor that takes an optional
source
parameter, which allows you to create a new Person
instance from an
object. It also has a static createFrom
method that can create a Person
instance from a string or object.
You may also notice that comments in the Go struct are kept in the generated JavaScript code! This can be helpful for understanding the purpose of the fields and methods in the generated models and should be picked up by your IDE.
Using Bound Models
Here’s an example of how you can use the generated JavaScript Person
type in
your frontend code:
In this example, we import the generated Person
type from the models
module.
We create a new instance of Person
, set its Name
property, and pass it to
the Greet
method.
Using bound models allows you to work with complex data structures and seamlessly pass them between the frontend and backend of your Wails application.
Using Typescript
To generate TypeScript bindings instead of JavaScript, you can use the -ts
flag:
This will generate TypeScript files in the frontend/bindings
directory:
Directoryfrontend/bindings
Directorymain
- greetservice.ts
- index.ts
- models.ts
The generated files include greetservice.ts
, which contains the TypeScript
code for the bound struct and its methods, and models.ts
, which contains the
TypeScript types for the bound models:
Using TypeScript bindings provides type safety and improved IDE support when working with the generated code in your frontend.
Using context.Context
When defining service methods in Go, you can include context.Context
as the
first parameter. The runtime will automatically provide a context when the
method is called from the frontend.
The context provides several powerful features:
-
Cancellation Support: Long-running operations can be cancelled from the frontend, which will raise an error through the Promise chain.
-
Window Information: You can determine which window made the call using these context keys:
application.WindowNameKey
- Returns the name of the calling windowapplication.WindowIDKey
- Returns the ID of the calling window
Here are some examples:
From the frontend, these methods can be called normally. If you need to cancel a long-running operation, the Promise will be rejected with the cancellation error: