Categorygithub.com/discoverkl/gots
repositorypackage
0.3.3
Repository: https://github.com/discoverkl/gots.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

Gots

Call remote Go methods directly from TypeScript through WebSocket.

Helloword

Try yourself:

go run github.com/discoverkl/gots-examples/[email protected]

Source code:

package main

import (
	"bufio"
	"embed"
	"errors"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/discoverkl/gots/ui"
)

//go:embed index.html
var www embed.FS

func add(a, b int) int {
	return a + b
}

func main() {
	app := ui.New(
		ui.Mode(promptRunMod()),
		ui.Root(www),
		ui.OnlineAddr(":8000"),
	)
	app.BindFunc("add", add)
	app.Run()
}

func promptRunMod() string {
	for {
		fmt.Print(promptText)
		ch, _, err := bufio.NewReader(os.Stdin).ReadRune()
		if err != nil {
			if errors.Is(err, io.EOF) {
				fmt.Println()
				os.Exit(0)
			}
			log.Fatal(err)
		}
		switch ch {
		case '1', 10:
			return "app"
		case '2':
			return "page"
		case '3':
			return "online"
		case 'q':
		default:
			os.Exit(0)
		}
	}
}

const promptText = `
*** Commands ***

1: LocalApp - start a local web server, open its' serving url within a native app
2: LocalPage - start a local web server, open its' serving url with your default web browser
3: Online   - run a online web server

Please enter (1-3)? `
<!DOCTYPE html>

<html>
    <head><title>Go and TypeScript</title></head>
    <body style="overflow: hidden;">
        <h1>Hello World!</h1>

        <p style="font-size: 40px;" id="sum"></span>

        <!-- Step 1: export Gots to window object -->
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/ts2go.js"></script>

        <script>
          function rand() {
            return Math.floor(Math.random() * 100);
          }
          async function main() {
            // Step 2: create a rpc client
            const api = await Gots.getapi()

            // Step 3: call remote go function: add
            let a = rand(), b = rand();
            document.getElementById("sum").innerText = a + " + " + b + " = " + await api.add(a, b)
          }
          main()
        </script>
    </body>

</html>