25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2021 Andrew Diamond
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Golang Webview Example
2 |
3 | This tiny app shows how to use the Go webview package at https://github.com/webview/webview. The package itself has very little documentation at https://pkg.go.dev/github.com/zserge/webview. It's basically just a wrapper around the various built-in browser renderers on Windows, Mac, and Linux.
4 |
5 | This app demonstrates the following, with simple, well-commented code:
6 |
7 | 1. How to load a start page.
8 | 2. How to call Go code from JavaScript.
9 | 3. How to call JavaScript code from Go.
10 |
11 | To run this, make sure you have Go 1.13 or higher. Clone this repo, cd into the top-level directory, then run `go run main.go`.
12 |
13 | This has been tested on Mac OS Catalina, but should run on Windows and most flavors of Linux as well.
14 |
15 | ## Why?
16 |
17 | We're evaluating webview as a possible alternative to Electron. In addition to being resource intensive, Electron requires we write everything in JavaScript, which means:
18 |
19 | * a huge number of dependencies
20 | * weekly dependency updates via Dependabot, often with breaking changes
21 | * forced use of Node's async operations even when async is not appropriate
22 |
23 | Our Electron app has about 2000 dependencies, while similar functionality in Go requires around 20 dependencies, most of which are more stable, better tested, and better documented.
24 |
25 | For ease of getting started, Electron is a big win. For long-term maintenance, it's not. At all. In fact, it's a huge timesuck.
26 |
27 | Webview's benefits:
28 |
29 | * easy compile
30 | * fewer and more stable dependencies
31 | * smaller binary (<20 MB on Mac vs. Electron's 285 MB)
32 | * less memory usage (<10 MB on startup, vs. Electron's 150 MB)
33 | * more sane and stable ecosystem (Go vs. npm/yarn)
34 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "path"
6 | "runtime"
7 | "strings"
8 | "time"
9 |
10 | "github.com/webview/webview"
11 | )
12 |
13 | var w webview.WebView
14 |
15 | func main() {
16 | debug := true
17 | w = webview.New(debug)
18 | defer w.Destroy()
19 | w.SetTitle("WebView Example with Bindings")
20 | w.SetSize(800, 600, webview.HintNone)
21 |
22 | // Load the start page, index.html
23 | w.Navigate("file://" + pathToStartPage())
24 |
25 | // Expose the Go function printSomething as "ps"
26 | // in our WebView window. A JavaScript call to "ps"
27 | // will invoke the Go function printSomething.
28 | w.Bind("ps", printSomething)
29 |
30 | // Run the app.
31 | w.Run()
32 | }
33 |
34 | // printSomething prints the name from the JavaScript form
35 | // to STDOUT and back to the WebView HTML page.
36 | func printSomething(name string) {
37 | now := time.Now().Format(time.Stamp)
38 | fmt.Println(textMessage(name, now))
39 | w.Eval(fmt.Sprintf("setDivContent('output', '%s')", htmlMessage(name, now)))
40 | }
41 |
42 | // Returns a plain text message to display in the console.
43 | func textMessage(name, time string) string {
44 | return fmt.Sprintf(">>> [%s] Hello, %s.", time, name)
45 | }
46 |
47 | // Returns an HTML message to display in the webview.
48 | func htmlMessage(name, time string) string {
49 | escapedName := strings.ReplaceAll(name, "'", "'")
50 | return fmt.Sprintf(`Hello, %s. The current time is %s. Check your console window for a message.`, escapedName, time)
51 | }
52 |
53 | // pathToStartPage returns the absolute path the index.html page
54 | // we want to show when the app starts up. This works when we're
55 | // running the app through 'go run main.go' because runtime.Caller()
56 | // returns the path of this source file, not the path of the temp
57 | // file created by 'go run'.
58 | func pathToStartPage() string {
59 | _, currentFile, _, _ := runtime.Caller(0)
60 | dir := path.Dir(currentFile)
61 | return path.Join(dir, "index.html")
62 | }
63 |
--------------------------------------------------------------------------------