dailydrop.hrbrmstr.dev, to golang

Wails: Coding in Harmony, Not in Distress!

The WPEs are b a c k!

We’re going to be doing a bit with the Wails project this year, so let’s start off nice and slow with a small project to get our feet wet before diving in head first.

Subscribe

Wails…who dis?

https://kagi.com/proxy/1002866_1.jpg?c=yj8bAX9r2L_88xABK6cI9d8B1rJdGat1itBlo-TIavloGGA4HLCOtF-ME2oVH2LbO1w1AIy4nUMx1-OPgtexrCyzS3K4TRB3B-GZs59m9BV-Q1CZhkCdrWMAI2WLYBUlWQ_AaGkJ9ciLwxjGcJl7YrTk6YkUuBRk_H_gIed_zA4im9c_7Gmf6O2xGmiSYyWln7anWa3GD-QKJByuRVNFlqp0233YWseDhhgPDv8kdb8%3D

Wails is a project that enables you to write desktop applications using Go and web technologies. It’s a lightweight and fast Electron alternative for Go, allowing you to build applications with the flexibility and power of Go, combined with a rich, modern frontend. Wails uses a purpose-built library for handling native elements such as Window, Menus, Dialogs, etc., so you can build good-looking, feature-rich desktop applications.

The amount of Go and JavaScript required is minimal, at first, and you can 100% get by with Kagi, Perplexity, or just some keen internet searches (and poking through the Drop history for Go and JavaScript resources). I’ll prove this with the app you’re going to modify.

Wails use of these technologies means you can (for the most part) turn any web app you make into something that you can distribute as an actual app to folks. While I’m more of a fan of native apps, this can save a ton of time and reduce cognitive load…something I think we’ll all need this year.

Getting Started with Wails

https://hrbrmstrsdailydrop.files.wordpress.com/2024/01/image-5.png?w=1024To start with Wails, you need to install it first. You can install Wails using the Go install command:

$ go install github.com/wailsapp/wails/v2/cmd/wails@latest

After that install finishes, you can validate it with:

$ wails doctor

In the wall of output you should see “SUCCESS” near the end.

Now, we can initialize a new project!

For this tutorial, we will use the Lit template:

$ wails init -n wails-tutorial -t lit

As longtime Drop readers know, Lit provides a very lightweight and sane wrapper over standard Web Components.

Now, do:

$ cd wails-tutorial$ wails dev

A bunch of text will scroll by and you should see an app screen that lets you enter in some text that will be parroted back at you.

The directory structure looks like this:

wails-tutorial├── README.md├── app.go├── build│   ├── README.md│   ├── appicon.png│   ├── darwin│   │   ├── Info.dev.plist│   │   └── Info.plist│   └── windows│       ├── icon.ico│       ├── info.json│       ├── installer│       │   ├── project.nsi│       │   └── wails_tools.nsh│       └── wails.exe.manifest├── frontend│   ├── dist│   │   └── gitkeep│   ├── index.html│   ├── package.json│   ├── src│   │   ├── assets│   │   │   ├── fonts│   │   │   │   ├── OFL.txt│   │   │   │   └── nunito-v16-latin-regular.woff2│   │   │   └── images│   │   │       └── logo-universal.png│   │   ├── my-element.js│   │   └── style.css│   ├── vite.config.js│   └── wailsjs│       ├── go│       │   └── main│       │       ├── App.d.ts│       │       └── App.js│       └── runtime│           ├── package.json│           ├── runtime.d.ts│           └── runtime.js├── go.mod├── go.sum├── main.go└── wails.json

Here are the core files we’ll be tweaking:

  • app.go contains the Go back end functions we’ll be exposing to the JavaScript front end
  • frontend/index.html is the entry point for the app
  • my-element.js is the Lit web component that handles the interactivity (we’ll be renaming this).

In the default app, Greet in app.go is called by my-element.js. Here’s what that function looks like:

func (a *App) Greet(name string) string {  return fmt.Sprintf("Hello %s, It's show time!", name)}

The func (a *App) Greet(name string) string is a function signature the Wails dev tools looks for. It will automagically generate glue code for the JavaScript parts, and you can use complex function parameters and return values. This one takes in a string, surrounds it with some basic text, then returns it.

You should take some time to change what’s in the Sprintf, rename Greet (and use the new imported name in the my-element.js) and tweak what the render() returns in my-element.js before continuing. It might also be a good idea to read through the official walkthrough.

Making A “Real” App

https://hrbrmstrsdailydrop.files.wordpress.com/2024/01/image-4.png?w=1024Rather than blather a bunch (it is your WPE, after all), I’m going to link you to Codeberg where there’s a modified version of this template project that:

  • uses a third-party Go module for fetching OpenGraph tags from a website
  • displays parts of those tags using Tachyons for the CSS framework.

The core change to app.go is this function:

func (a *App) FetchOpenGraphTags(postURL string) (map[string]string, error) {fmt.Println("FOGT:", postURL)og, err := opengraph.Fetch(postURL)fmt.Printf("OpenGraph: %+vnError: %vn", og, err)return map[string]string{"title":       og.Title,"description": og.Description,"url":         og.URL.String(),}, err}

I added some printing in there so you can see messages appear in the terminal console while the app is running.

When you run wails dev the glue code is, again, automagically generated so the JavaScript side can call that function via:

async fetchOG() {  let thisURL = this.shadowRoot.querySelector('input#url').value  console.log("fetchOG")  FetchOpenGraphTags(thisURL).then(result => {    console.log('fetch opengraph tags:', result)    this.tags = result;    this.requestUpdate();  });}// I’m showing this here, but we’ll talk about it in the next WPEasync connectedCallback() {  super.connectedCallback();}

The fetchOG function is called in render() when the button is pressed:

<button @click=${this.fetchOG} class="btn">Render</button>

You can use Developer Tools from the running app to watch the console messages go by (very useful for debugging).

Your Mission

Your goal is to (a) successfully get this modified app running and (b) utilize more OG tag fields and display the entire set of OG tags in a much nicer way. Extra points for adding another Golang back end function that you need to call from the JS side.

FIN

Today’s WPE sets a solid foundation for future ones, and we’ll be having tons of fun with it as we layer in DuckDB, SQLite, WebR, Pyodide, and more over the coming weeks/months. Give a shout-out or reply to this post if you have any questions/issues.

The first Bonus Drop goes out this weekend to paid subscribers! (The refunds from the transition from Nazistack should be out soon…I keep checking on the status and will follow up next week if there’s a delay.)

Remember, you can follow and interact with the full text of The Daily Drop’s free posts on Mastodon via @dailydrop.hrbrmstr.dev@dailydrop.hrbrmstr.dev ☮️

https://dailydrop.hrbrmstr.dev/2024/01/05/drop-399-2024-01-05-weekend-project-edition/

image/png
image/png

schizanon, (edited ) to random
@schizanon@mas.to avatar

It's cool that is integrating with .

It feels like we've been circling around this design pattern for decades; they've been called Observables, Pub/Sub, State, Context, etc.

STOKED to hear that there's discussion around standardizing and adding Signals to the !

https://www.youtube.com/live/ri9FEl_hRTc?si=-oztbFGgLG0f9aXJ&t=1636

schizanon,
@schizanon@mas.to avatar

"in a way you can think of a lit element is like a computed effect of a signals although our system kinda works like a push system instead of signals that often have a pull system" ~ @justinfagnani

schizanon, to javascript
@schizanon@mas.to avatar

It's super cool that is supporting standard , but I'm still not going to use them!

Maybe one day when they don't need to be transpiled anymore.

schizanon, (edited ) to CSS
@schizanon@mas.to avatar

I think that solves 's problems and is right to scope styles to the element.

schizanon, (edited ) to react
@schizanon@mas.to avatar

is getting a "context" api https://www.npmjs.com/package/@lit/context

It seems to work like , but it's an open standard that other frameworks can apparently implement.

vanilla, to webdev
@vanilla@social.spicyweb.dev avatar

“React Is Not Modern Web Development”

@jaredwhite’s personal thoughts on a great entry by @collinsworth into the growing body of work which details why greenfield projects are better served by other frameworks…or none at all.

https://thathtml.blog/2023/08/react-is-not-modern/

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • kavyap
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • InstantRegret
  • GTA5RPClips
  • Youngstown
  • everett
  • slotface
  • rosin
  • osvaldo12
  • mdbf
  • ngwrru68w68
  • megavids
  • cubers
  • modclub
  • normalnudes
  • tester
  • khanakhh
  • Durango
  • ethstaker
  • tacticalgear
  • Leos
  • provamag3
  • anitta
  • cisconetworking
  • lostlight
  • All magazines