--- title: QR Code Service description: Create a QR code service to learn about Wails services sidebar: order: 1 --- import { Steps } from "@astrojs/starlight/components"; import {Image } from 'astro:assets'; import qr1 from "../../../assets/qr1.png"; A **service** in Wails is a Go struct that contains business logic you want to make available to your frontend. Services keep your code organized by grouping related functionality together. Think of a service as a collection of methods that your JavaScript code can call. Each public method on the service becomes callable from the frontend after generating bindings. In this tutorial, we'll create a QR code generator service to demonstrate these concepts. By the end, you'll understand how to create services, manage dependencies, and connect your Go code to the frontend.
1. ## Create the QR Service file Create a new file called `qrservice.go` in your application directory: ```go title="qrservice.go" package main import ( "github.com/skip2/go-qrcode" ) // QRService handles QR code generation type QRService struct { // We can add state here if needed } // NewQRService creates a new QR service func NewQRService() *QRService { return &QRService{} } // Generate creates a QR code from the given text func (s *QRService) Generate(text string, size int) ([]byte, error) { // Generate the QR code qr, err := qrcode.New(text, qrcode.Medium) if err != nil { return nil, err } // Convert to PNG png, err := qr.PNG(size) if err != nil { return nil, err } return png, nil } ``` **What's happening here:** - `QRService` is an empty struct that will hold our QR code generation methods - `NewQRService()` is a constructor function that creates a new instance of our service - `Generate()` is a method that takes text and a size, then returns the QR code as a PNG byte array - The method returns `([]byte, error)` following Go's convention of returning errors as the last value - We use the `github.com/skip2/go-qrcode` package to handle the actual QR code generation
2. ## Register the Service Creating a service isn't enough - we need to **register** it with the Wails application so it knows the service exists and can generate bindings for it. Registration happens in `main.go` when you create your application. You pass your service instances to the `Services` option: ```go title="main.go" ins={7-9} func main() { app := application.New(application.Options{ Name: "myproject", Description: "A demo of using raw HTML & CSS", LogLevel: slog.LevelDebug, Services: []application.Service{ application.NewService(NewQRService()), }, Assets: application.AssetOptions{ Handler: application.AssetFileServerFS(assets), }, Mac: application.MacOptions{ ApplicationShouldTerminateAfterLastWindowClosed: true, }, }) app.Window.NewWithOptions(application.WebviewWindowOptions{ Title: "myproject", Width: 600, Height: 400, }) // Run the application. This blocks until the application has been exited. err := app.Run() // If an error occurred while running the application, log it and exit. if err != nil { log.Fatal(err) } } ``` **What's happening here:** - `application.NewService()` wraps your service so Wails can manage it - We call `NewQRService()` to create an instance of our service - The service is added to the `Services` slice in application options - Wails will now scan this service for public methods to make available to the frontend
3. ## Install Dependencies We referenced the `github.com/skip2/go-qrcode` package in our code, but we haven't actually downloaded it yet. Go needs to know about this dependency and download it to your project. Run this command in your terminal from your project directory: ```bash go mod tidy ``` **What's happening here:** - `go mod tidy` scans your Go files for import statements - It downloads any missing packages (like `go-qrcode`) and adds them to `go.mod` - It also removes any dependencies that are no longer used - This ensures your project has all the code it needs to compile successfully You should see output indicating that the QR code package has been downloaded and added to your project.
4. ## Generate the Bindings To call these methods from your frontend, we need to generate bindings. You can do this by running `wails generate bindings` in your project root directory. :::note The very first time you ever run this in a project, the bindings generator does a thorough analysis of your code and dependencies. This can sometimes take a little longer than expected, however subsequent runs will be much faster. ::: Once you've run this, you should see something similar to the following in your terminal: ```bash % wails3 generate bindings INFO Processed: 337 Packages, 1 Service, 1 Method, 0 Enums, 0 Models in 740.196125ms. INFO Output directory: /Users/leaanthony/myproject/frontend/bindings ``` You should notice that in the frontend directory, there is a new directory called `bindings`: ```bash frontend/ └── bindings └── changeme ├── index.js └── qrservice.js ``` :::tip[Pro Tip] When you build your application using `wails3 build`, it will automatically generate bindings for you and keep them up to date. :::
5. ## Understanding the Bindings Let's look at the generated bindings in `bindings/changeme/qrservice.js`: ```js title="bindings/changeme/qrservice.js" // @ts-check // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT /** * QRService handles QR code generation * @module */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: Unused imports import {Call as $Call, Create as $Create} from "@wailsio/runtime"; /** * Generate creates a QR code from the given text * @param {string} text * @param {number} size * @returns {Promise & { cancel(): void }} */ export function Generate(text, size) { let $resultPromise = /** @type {any} */($Call.ByID(3576998831, text, size)); let $typingPromise = /** @type {any} */($resultPromise.then(($result) => { return $Create.ByteSlice($result); })); $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); return $typingPromise; } ``` We can see that the bindings are generated for the `Generate` method. The parameter names have been preserved, as well as the comments. JSDoc has also been generated for the method to provide type information to your IDE. :::note It's not necessary to fully understand the generated bindings, but it's important to understand how they work. ::: The bindings provide: - Functions that are equivalent to your Go methods - Automatic conversion between Go and JavaScript types - Promise-based async operations - Type information as JSDoc comments :::tip[Typescript] The bindings generator also supports generating Typescript bindings. You can do this by running `wails3 generate bindings -ts`. ::: The generated service is re-exported by an `index.js` file: ```js title="bindings/changeme/index.js" // @ts-check // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT import * as QRService from "./qrservice.js"; export { QRService }; ``` You may then access it through the simplified import path `./bindings/changeme` consisting just of your Go package path, without specifying any file name. :::note Simplified import paths are only available when using frontend bundlers. If you prefer a vanilla frontend that does not employ a bundler, you will have to import either `index.js` or `qrservice.js` manually. :::
6. ## Use Bindings in Frontend Now we can call our Go service from JavaScript! The generated bindings make this easy and type-safe. Update `frontend/src/main.js` to use the new bindings: ```js title="frontend/src/main.js" import { QRService } from './bindings/changeme'; async function generateQR() { const text = document.getElementById('text').value; if (!text) { alert('Please enter some text'); return; } try { // Generate QR code as base64 const qrCodeBase64 = await QRService.Generate(text, 256); // Display the QR code const qrDiv = document.getElementById('qrcode'); qrDiv.src = `data:image/png;base64,${qrCodeBase64}`; } catch (err) { console.error('Failed to generate QR code:', err); alert('Failed to generate QR code: ' + err); } } export function initializeQRGenerator() { const button = document.getElementById('generateButton'); button.addEventListener('click', generateQR); } ``` **What's happening here:** - We import `QRService` from the generated bindings - `QRService.Generate()` calls our Go method - it returns a Promise, so we use `await` - The Go method returns `[]byte`, which Wails automatically converts to a base64 string for JavaScript - We create a data URL with the base64 string to display the PNG image - The `try/catch` block handles any errors from the Go side (like invalid input) - If our Go code returns an error, the Promise rejects and we catch it here Now update `index.html` to use the new bindings in the `initializeQRGenerator` function: ```html title="frontend/src/index.html" QR Code Generator
``` Run `wails3 dev` to start the dev server. After a few seconds, the application should open. Type in some text and click the "Generate QR Code" button. You should see a QR code in the center of the page: QR Code

7. ## Alternative Approach: HTTP Handler So far, we have covered the following areas: - Creating a new Service - Generating Bindings - Using the Bindings in our Frontend code **Why use an HTTP handler?** Method bindings work great for data operations, but there's an alternative approach for serving files, images, or other media. Instead of converting everything to base64 and sending it through bindings, you can make your service act like a mini web server. This is useful when: - You're serving images, videos, or large files - You want to use standard HTML `` or `