gox

command module
v0.0.0-...-4828a9b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 8 Imported by: 0

README

gox

gox is an extension of Go's syntax that let's you write HTML-style tags directly in your source code.
In other words, it's JSX for Go.

Write HTML-style tags directly in your Go source, and have them get transpiled into Vecty components that can be run with GopherJS or WebAssembly.

Okay take a look:

package main

import "github.com/gopherjs/vecty"

type Page struct{
	vecty.Core
}

func (w *Page) Render() vecty.ComponentOrHTML {
	return <body>
		<div class="amazing">
			<h1>gox</h1>
			<span class={"you could put dynamic content here"}/>
			yeah you can do bare words too
		</div>
	</body>
}

func main() {
	vecty.RenderBody(new(Page))
}

Quickstart

git clone github.com/8byt/gox
cd gox/
go run . examples/

go install github.com/hajimehoshi/wasmserve

wasmserve github.com/8byt/gox/examples/readme_1/
google-chrome http://localhost:8080

Why?

Four big reasons:

  • It would be nice to have type safety, but I'm unwilling to write longform Vecty components
  • It would be nice to know how Go parsing works
  • I would like to learn Go by modifying its AST (Danny's reason)
  • I want to write frontend code, but I don't want JS (Eric's reason)

How?

We basically vendored the Go parser/scanner/AST/etc. and just modified it until it fit our needs.

Wot?

Here's a more complicated example portion of a .gox file.

func (p *PageView) renderItemList() vecty.ComponentOrHTML {
	var items vecty.List
	for i, item := range store.Items {
		if (store.Filter == model.Active && item.Completed) || (store.Filter == model.Completed && !item.Completed) {
			continue
		}
		items = append(items, <ItemView Index={i} Item={item} />)
	}

	return <section class="main">
		<input
			id="toggle-all"
			class="toggle-all"
			type="checkbox"
			checked={store.CompletedItemCount() == len(store.Items)}
			onChange={p.onToggleAllCompleted}/>
		<label for="toggle-all">Mark all as complete</label>
		<ul class="todo-list">
			{items}
		</ul>
	</section>
}

from our TodoMVC implementation

alright, I'm convinced, get me started

Wow! Okay I don't think we thought that would happen.

For now, clone this repo, and build it.

Use gox <directory> to convert .gox files into .go files (they stay in the same directory)

GopherJS should take care of the rest, use Vecty's docs and GopherJS's docs to learn more. We use gopherjs serve and things magically get transpiled again.

If you want to make this process better, we'd be happy to consider your ideas/PRs.

Thanks,

Eric and Danny

License

All modifications are MIT

Original Go code is all BSD

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package ast declares the types used to represent syntax trees for Go packages.
Package ast declares the types used to represent syntax trees for Go packages.
Package parser implements a parser for Go source files.
Package parser implements a parser for Go source files.
Package printer implements printing of AST nodes.
Package printer implements printing of AST nodes.
Package scanner implements a scanner for Go source text.
Package scanner implements a scanner for Go source text.
Package token defines constants representing the lexical tokens of the Go programming language and basic operations on tokens (printing, predicates).
Package token defines constants representing the lexical tokens of the Go programming language and basic operations on tokens (printing, predicates).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL