Golang: Create a Web-View App for any platform

Reza Torkaman Ahmadi
4 min readNov 19, 2018

One of the greatest programming languages that I recently got in love with, is Golang which is an awesome compiled-language that give developer so much power to make cross-platform binaries ready to use.

Recently I needed to create a web-View app for one of my company web projects. So while I was doing it, I tried to make a use of this experience and share it with anybody that is interested with this topic.

First make sure you have GO installed in your system. I use Arch Linux Distro, But any Distro you’re using is fine. Just visit GO website and install it. For me it’s just a simple command as below.

$ sudo pacman -S go

Now that you have go installed, you should set your GOPATH environment variable, because any GO program needs to have a valid GOPATH. You can create your project in GOPATH directory of your system, But i like to have separated $GOPATH directory for each of my projects (By the way, I’m new to this, So i guess saying my projects is some sort of funny. That word is for experts, not me:)

Now Create a project folder and define it as your GOPATH directory. Like below:

$ mkdir ~/web-view
$ cd ~/web-view
$ export GOPATH=~/web-view

To create a web view with browser embeded in it, you should use zserge library, which will you will find in github.com/zserge/webview path. Just install it with command below:

$ go get github.com/zserge/webview

Now that you have this library installed, create a file main.go .

$ touch src/web-view/main.go

Now open it and with your favorite Text editor or IDE and write below code in it:

package main

import (
"fmt"
"github.com/zserge/webview"
)

func main() {

webView := webview.New(webview.Settings{
Title: "My test web view app",
URL: "http://google.com",
Width: 1000,
Height: 800,
Resizable: true,
Debug: true,
ExternalInvokeCallback: nil,
})

webView.SetFullscreen(true)
webView.Run()

}

Now we have a Web-View which will be pointing to http://google.com . Now build your binary and run it.

$ go build main.go
$ ./main

And you will see a full-screen browser will pop up which will be pointing to http://google.com .

If you right-click with mouse, you will notice that browser options will show up. to disable them you should inject some java-script to it. like this:

webView.Dispatch(func() {
// Inject JS to disable right click and inspect element
webView.Eval(fmt.Sprintf(`
$(document).on("contextmenu",function(e){
e.preventDefault();
});
`
))
})

This will disable F12 button and right-click of mouse.

Build it for Other Platforms

Now that our code is ready, we can build it for other platforms like Mac, Windows and …

To cross compile a GO program you should do as follow:

  • set GOOS and GOARCH env variables to be the values for the target operating system
  • and run GOOS=<target-operating-system> GOARCH=<target-architecture> go build

list of different platforms and their assigned GOOS and GOARCH variables is on link https://golang.org/docs/install/source#environment . But For my work I needed windows 64 bit binary.

But wait, this library zserge is using CGO…

CGO lets Go packages call C code. Given a Go source file written with some special features, cgo outputs Go and C files that can be combined into a single Go package.

And because we are building binary from linux to windows, we Should Install mingw.

Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems. It has forked it in 2007 in order to provide support for 64 bits and new APIs. It has since then gained widespread use and distribution.

So first you need to install GCC Compiler needed files for windows 64 bit, In different distribution based on package manager command will be different:

# Ubuntu
$ sudo apt-get install gcc-mingw-w64
# Centos
$ sudo yum install mingw64-gcc
# Arch
$ sudo pacman -S mingw-w64-gcc

Install it and run command below to create binary for windows.

$  GOOS=windows GOARCH=amd64 go build

If everything goes right, you will have a binary ready to use in windows. But it will give a result as below:

go build github.com/zserge/webview: build constraints exclude all Go files in ~/web-view/src/github.com/zserge/webview

This is because this library zserge , is using CGO, so we have to cross compile it with some mingw toolchain, like CGO_ENABLED=1 and …

To solve this, use command below:

64bit
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -ldflags "-H windowsgui"
32bit
GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ go build -ldflags "-H windowsgui"

Now you have a .exe file ready to use in windows.

You can run file command to make sure that everything went ok.

$ file main.exe
main.exe: PE32+ executable (GUI) x86-64, for MS Windows

Final Notes

If you had a problem installing mingw-w64 packages or compilation time got so long, try to install pre-compiled version of it. I used mingw-w64-gcc-bin for my system.

Also If you have any Problem with pgp key validation, receive key with command below and re-run build procedure again.

$ pgp --recv <pgp-key in error response>

Once done, the gpg verification should work with makepkg for that KEYID.

Hope this article was useful. peace :)

--

--

Reza Torkaman Ahmadi

CTO & Co-Founder @ CPOL; A CTF enthusiast & believer in rough consensus and running codes. A person who loves to learn about whatever that makes him excited;)