Golang fyne Where to click? :-(

Fyne is an open-source library for very easy and fast creation of cross-platform Golang applications.

Here you can completely understand what fyne is for, on my own I can say that this is a very simple library to use, you don’t need any knowledge to start writing programs with a graphical interface on it

You can take a peek there rather slowly and only talk about the base completely, so you can use it as a reference when you quickly sketch out your first interface:

It shows how to do it very well with an example, the trick of fyne is not to use the standard set of tools presented in the library, but to write the corresponding classes based on them for the required interfaces, plus I can add that the code in the library is very easy to read (not what is stl c++😩) and this code is not very much, it’s pretty quick to figure it out:

If you don’t like to watch, but like to read, then there are a couple of articles on Habré about how to write under fyne, you can also get some base there, but I still advise you to watch the second link on YouTube, at least at speed 2 and skipping a big part for review, there is the best material that I found on the internet

Now that you and I got acquainted with the database and wrote something, the question arises where to click so that we can throw off the thing that we coded, but here a problem arises, everyone has different systems, I can single out three main ones under which you can compile windows, linux (I checked everything on ubuntu, but it seems that one of my friends has some kind of hacker linux, but he launched my binary under linux) and macOS (the most problematic😩) well, maybe an android, but I don’t have it, I guess it’s not difficult there , you can forget about iOS because there is only through the App Store, and we are not serious guys making serious applications, we are two abobuses, and we want to throw off a joke to our friends (if you are a serious guy, go study gotk4 better, fyne is not very optimal)

First download Go

macOS

brew install go

linux

sudo apt install golang

Let’s write the first hello main, https://developer.fyne.io/started/hello

package main

import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	w := a.NewWindow("Hello World")

	w.SetContent(widget.NewLabel("Hello World!"))
	w.ShowAndRun()
}

Next, we generate go.mod (we will write this command in the project directory)

go mod init hello_world

Now let’s download all the necessary dependencies

 go mod tidy

It should now be something like this

Now let’s run

go run main.go

cheers for victory

Well, that’s all, we have learned, although I kind of said something about different systems and so on, that everything is complicated, but here it is so simple, something is not right. Let’s remember at a basic level how we run applications

go build -o hello_world

We used the -o flag to tell what form the binary will be in.

Here is our hello_world binary, it starts with ./hello_world, it’s not very convenient, plus not everyone has the same os and processor architecture as you, so not everyone will be able to run, and most of the people on windows and they are not familiar with a terminal, so we need to compile for different systems and as a result we will get several binaries.

Go is cross-platform and this is its plus, you can build from one platform to another. Usually, Go is compiled for other platforms as follows:

env GOOS=windows GOARCH=amd64 go build -o hello_world.exe

And all? So simple? Of course not

here is what is written on macOS amr64
here is what is written on macOS amr64

So you have to compile through the docker, but there is more and more to dig, and you and I don’t want to bother, we want to quickly write and send to friends. fyne offers its own solution to this problem

go get fyne.io/fyne/v2/cmd/fyne

Further, according to the idea of ​​developers, we should immediately write through the fyne command, but this does not work everywhere and always

go install fyne.io/fyne/v2/cmd/fyne
#~/go/bin/fyne вот где сейчас fyne
PATH=$PATH:~/go/bin #можешь отредачить .bash_profile

Now the fyne command is available to us, let’s download and throw any .png into the project and call it Icon.png

fyne install

And that’s it, now it’s installed, //add

fyne package
fyne package -os windows

But we’ll get something like this

# runtime/cgo
gcc_libinit_windows.c:8:10: fatal error: 'windows.h' file not found

error building application: exit status 2

Therefore, we will have to download all the dependencies for other OS, but this is not the best solution, it is best to compile through the docker, fyne has a tutorial on how to do this fyne-cross

go get github.com/fyne-io/fyne-cross
go install github.com/fyne-io/fyne-cross

Now we can use the fyne-cross command (don’t forget to download docker)

fyne-cross windows -arch amd64 -name hello_world
fyne-cross linux -arch amd64 -name hello_world

fyne-cross created a fine-cross folder, which in my opinion looks unaesthetic, below we will get rid of this folder and save everything to the download directory

mkdir -p download
mkdir -p download/linux-amd64
mkdir -p download/windows-amd64
mv fyne-cross/dist/linux-amd64/hello_world.tar.xz download/linux-amd64/hello_world.tar.xz 
mv fyne-cross/dist/windows-amd64/hello_world.zip download/windows-amd64/hello_world.zip 

Now let’s move on to macOS (unfortunately, building for it is only supported if you have this os)

fyne-cross darwin -arch=amd64 -name hello_world -app-id valerijhegaj.hello_world
fyne-cross darwin -arch=arm64 -name hello_world -app-id valerijhegaj.hello_world

In general, in order for the application to work normally on macOS, it needs to be signed and notarized, but this can take a lot of time, so I found a small hack, first we will make a dmg image, as macOS developers usually do

brew install node
npm install create-dmg
mkdir -p download/macos-amd64
mkdir -p download/macos-arm64
create-dmg fyne-cross/dist/darwin-amd64/hello_world.app download/macos-amd64
create-dmg fyne-cross/dist/darwin-amd64/hello_world.app download/macos-arm64
mv download/macos-amd64/hello_world\ 1.0.dmg download/macos-amd64/hello_world.dmg
mv download/macos-arm64/hello_world\ 1.0.dmg download/macos-arm64/hello_world.dmg

In general, you can not build it separately for arm, because Mac on apple silicon can run applications for x86-64, now let’s clean up the garbage

rm -rf fyne-cross

Wow victory? But not yet, now upload to the Internet, download .dmg from there and install the application

This happens on modern macOS, because when you first start the application, the system checks if it is signed and notarized, and if not, it quarantines it

sudo xattr -rd com.apple.quarantine /Applications/hello_world.app

This way you can lift the quarantine, and in the end we got everything we need, but every time it’s a chore to do it – it’s hard, unpleasant, so I wrote Makefile to automate it all.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *