Coder Social home page Coder Social logo

presleyhank / app Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maxence-charriere/go-app

0.0 2.0 0.0 27.7 MB

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

Home Page: https://app-demo-232021.appspot.com

License: MIT License

Go 90.66% JavaScript 6.08% CSS 2.62% HTML 0.63%

app's Introduction

ui demo

app

Circle CI Go build Go Report Card GoDoc

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

Install

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

# Goapp cli tool:
go get -u -v github.com/maxence-charriere/app/cmd/goapp

How it works

Project layout

root
├── cmd
│   ├── demo-server
│   │   └── main.go
│   └── demo-wasm
│       └── main.go
└── web
    ├── wasm_exec.js
    ├── style sheets...
    ├── images...
    └── etc...

This layout follows the project layout defined in golang-standards/project-layout:

  • The cmd directory contains the project main applications.
  • The demo 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.

// root/cmd/demo-wasm/main.go

package main

import (
    "log"

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

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="{{bind "Name"}}" autofocus>
</div>
    `
}

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

    app.DefaultPath = "/hello"

    if err := app.Run(); err != nil {
        log.Print(err)
    }
}

Server

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

// root/cmd/demo-server/main.go

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"

    "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 is 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:

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

See the full example code and the online demo.

Support

Requires Go 1.12.

Platform Chrome Edge Firefox Safari
Desktop
Mobile

Issues:

  • Go wasm currently triggers out of memory errors on mobile platforms (golang/go#27462).
  • Edge does not support TextEncoder which is used by the javascript support file.

app's People

Contributors

haraldnordgren avatar jacktuck avatar maxence-charriere avatar shanamaid avatar tectiv3 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.