app

Package to build GUI apps with Go, HTML and CSS.

Github星跟蹤圖

app

A WebAssembly framework to build GUI with
Go, HTML and
CSS.

It features:

Install

Requires Go 1.13

# Package sources + goapp CLI:
go get -u -v github.com/maxence-charriere/app/cmd/goapp

# Package sources only:
go get -u -v github.com/maxence-charriere/app/pkg/app

Getting started

cd $GOPATH/src          # go to your gopath sources (optional)
mkdir demo && cd demo   # create and go to your go package
goapp init -v           # init project layout
goapp run -v -b chrome  # run the app and launch the main page on chrome

How it works

Project layout

demo
└── cmd
    ├── demo-server
    │   ├── main.go
    │   └── web
    │       ├── style sheets...
    │       ├── images...
    │       └── etc...
    └── demo-wasm
        └── main.go
  • The cmd directory contains the project main applications.
  • The demo-wasm directory contains the app that is compiled in wasm and that will run in the browser.
  • The demo-server directory contains the server that serves the wasm app and its resources.
  • The web directory contrains the app resources like style sheets (css), images and other static resources.

Project layout can be initialized by running this command in the repository root.

goapp init -v

App

The app is the Go code compiled in web assembly and executed in the browser.

// cmd/demo-wasm/main.go

package main

import (
    "log"

    "github.com/maxence-charriere/app/pkg/app"
    "github.com/maxence-charriere/app/pkg/log"
)

type Hello struct {
    Name string
}

func (h *Hello) Render() string {
    return `
<div class="Hello">
    <h1>
        Hello
        {{if .Name}}
            {{.Name}}
        {{else}}
            world
        {{end}}!
    </h1>
    <input value="{{.Name}}" placeholder="What is your name?" onchange="Name" autofocus>
</div>
    `
}

func main() {
    app.Import(&Hello{})

    app.DefaultPath = "/hello"
    app.Run()
}

Server

The server serves the web assembly Go program and the other resources.

// cmd/demo-server/main.go

package main

import (
    "log"
    "net/http"
    "github.com/maxence-charriere/app"
)

func main() {
    http.Handle("/", &app.Handler{})

    if err := http.ListenAndServe(":3000", nil); err != nil {
        log.Fatal(err)
    }
}

Build

The whole project can be built with the
goapp
CLI tool.
goapp builds the server, the wasm app, imports the required javascript
support file and puts the pieces together to provide a ready to use project.

# Get the goapp CLI tool:
go get -u github.com/maxence-charriere/app/cmd/goapp

# Builds a server ready to serve the wasm app and its resources:
goapp build -v

# Launches the server and app in the default browser:
goapp run -v -b default

Once built, the directory tree should look like:

demo
└── cmd
    ├── demo-server
    │   ├── demo-server (server)
    │   ├── main.go
    │   └── web
    │       ├── goapp.wasm (app)
    │       ├── wasm_exec.js
    │       ├── images...
    │       └── etc...
    └── demo-wasm
        └── main.go

See a full example.

Live demo

Support, Platform, Chrome, Edge, Firefox, Safari, :-------, :----:, :--:, :-----:, :----:, Desktop, ✔, ✔*, ✔, ✔, Mobile, ✔, ✔, ✔, ✔, Issues:

  • Non Chromiun based Edge does not support TextEncoder which is used by the javascript support file provided by Go.

主要指標

概覽
名稱與所有者maxence-charriere/go-app
主編程語言Go
編程語言Go (語言數: 4)
平台
許可證MIT License
所有者活动
創建於2016-10-12 00:31:33
推送於2025-04-16 23:10:57
最后一次提交2025-03-24 16:00:46
發布數133
最新版本名稱v10.1.3 (發布於 )
第一版名稱1.0.0 (發布於 2018-02-03 11:23:18)
用户参与
星數8.7k
關注者數150
派生數388
提交數1.2k
已啟用問題?
問題數540
打開的問題數22
拉請求數467
打開的拉請求數20
關閉的拉請求數43
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?