WebForms Core (also referred to as WFC) is a server-oriented technology for manipulating web interfaces through commands.
This package is the GO implementation of the WebForms Core Commander, providing the WebForms class used to generate commands for WebFormsJS.
WebForms Core is developed and maintained by Elanat.
WebForms Core consists of two main parts:
- Commander — WebForms classes implemented for different programming languages. Commanders generate WebForms Core commands that describe operations on the HTML document.
- Executor — WebFormsJS, the client-side JavaScript library that receives and executes those commands in the browser. Its physical file name is
web-forms.js.
The Commander can run in different environments, including server-side applications, browsers, and WebAssembly, depending on the implementation.
The Executor is loaded into the HTML document and executes the commands generated by the Commander.
Install the package from GO module repository:
go get github.com/webforms-core/goThen load it:
import (
"github.com/webforms-core/go"
)The following example uses Go's standard net/http package to create a simple web application with WebForms Core.
package main
import (
"fmt"
"net/http"
"{your-project-name}/webformscore"
)
func main() {
http.Handle("/script/", http.StripPrefix("/script/", http.FileServer(http.Dir("script"))))
http.HandleFunc("/", handleForm)
http.ListenAndServe(":8080", nil)
}
func handleForm(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
name := r.FormValue("txt_Name")
backgroundColor := r.FormValue("txt_BackgroundColor")
fontSize := r.FormValue("txt_FontSize")
form := WebFormsCore.New()
var InputPlace = WebFormsCore.InputPlace
form.SetFontSize(InputPlace.Tag("form"), fontSize+"px")
form.SetBackgroundColor(InputPlace.Tag("form"), backgroundColor)
form.SetDisabled(InputPlace.Name("btn_SetBodyValue"), true)
form.AddTag(InputPlace.Tag("form"), "h3", "p")
form.SetText(InputPlace.Tag("h3"), "Welcome "+name+"!")
fmt.Fprint(w, form.Response())
return
}
fmt.Fprint(w, `<!DOCTYPE html>
<html>
<head>
<title>Using WebForms Core</title>
<script type="module" src="/script/web-forms.js"></script>
</head>
<body>
<form method="post" action="/" >
<label for="txt_Name">Your Name</label>
<input name="txt_Name" id="txt_Name" type="text" />
<br>
<label for="txt_FontSize">Set Font Size</label>
<input name="txt_FontSize" id="txt_FontSize" type="number" value="16" min="10" max="36" />
<br>
<label for="txt_BackgroundColor">Set Background Color</label>
<input name="txt_BackgroundColor" id="txt_BackgroundColor" type="text" />
<br>
<input name="btn_SetBodyValue" type="submit" value="Click to send data" />
</form>
</body>
</html>`)
}When the page receives a request, the server first checks whether the form has been submitted.
If the form has been submitted, the application:
- Reads the submitted form values.
- Creates an instance of the
WebFormsCoreclass. - Calls WebForms Core methods to define the required UI changes.
- Generates the WebForms Core response with
form.Response(). - Returns the generated commands to the browser without rendering the initial HTML view again.
For example:
form := WebFormsCore.New()
var InputPlace = WebFormsCore.InputPlace
form.SetFontSize(InputPlace.Tag("form"), fontSize+"px")
form.SetBackgroundColor(InputPlace.Tag("form"), backgroundColor)
form.SetDisabled(InputPlace.Name("btn_SetBodyValue"), true)
form.AddTag(InputPlace.Tag("form"), "h3", "p")
form.SetText(InputPlace.Tag("h3"), "Welcome "+name+"!")
form.Response()If the form has not been submitted, which is the case for the initial request, the complete HTML document is returned to the requester.
This allows the server to initially return the HTML document and subsequently return WebForms Core commands that WebFormsJS executes against the existing HTML DOM.
When WebForms Core commands need to be sent together with the initial HTML document, you can use ExportToHtmlComment(true) instead of Response().
ExportToHtmlComment(true) embeds the generated WebForms Core commands in an HTML comment. WebFormsJS detects these commands when the HTML document is loaded and executes them in the browser.
For example, a Go application using net/http can generate WebForms Core commands during the initial page load:
package main
import (
"fmt"
"net/http"
"github.com/webforms-core/go"
)
func main() {
http.Handle("/script/", http.StripPrefix("/script/", http.FileServer(http.Dir("script"))))
http.HandleFunc("/", handlePage)
fmt.Println("Server started at http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
func handlePage(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/" {
http.NotFound(w, r)
return
}
form := WebFormsCore.New()
form.SetBackgroundColor("<body>", "lightblue")
html := `<!DOCTYPE html>
<html>
<head>
<title>WebForms Core</title>
<script type="module" src="/script/web-forms.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>`
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, html+form.ExportToHtmlComment(true))
}In this case, the generated commands are embedded in the HTML response itself. When WebFormsJS loads, it detects the commands in the HTML comment and executes them against the DOM.
Use Response() when returning WebForms Core commands as a response to a request. Use ExportToHtmlComment(true) when the commands need to be included directly in the HTML document, such as during the initial page load.
WebForms Core can be used with Go to create WebAssembly modules that execute methods and generate WebForms Core commands.
The Go WebForms class acts as a Commander inside the WebAssembly module. It can generate WebForms Core responses that are processed by the WebFormsJS Executor in the browser.
Go WebAssembly uses the Go WebAssembly runtime provided by wasm_exec.js. The Go module can expose methods to JavaScript through the syscall/js package.
First, create a Go project and add the WebForms Core Go classes to your project.
The project can have the following structure:
your-project-name/
├── webformscore/
│ └── webforms.go
├── main.go
└── go.mod
The go.mod file can be configured as follows:
module your-project-name
go 1.25.5The Go WebAssembly module can then use the local WebForms Core package:
import (
"syscall/js"
WebFormsCore "your-project-name/webformscore"
)The following example exports several Go methods to JavaScript.
The add method performs a simple calculation, while the setData method creates a WebForms instance and generates commands that modify the HTML page. The getHtml method returns an HTML string that can be inserted into the page.
package main
import (
"syscall/js"
WebFormsCore "your-project-name/webformscore"
)
func add(this js.Value, args []js.Value) interface{} {
a := int32(args[0].Int())
b := int32(args[1].Int())
return a + b
}
func setData(this js.Value, args []js.Value) interface{} {
inputPlace := args[0].String()
text := args[1].String()
backgroundColor := args[2].String()
fontSize := args[3].String()
form := WebFormsCore.New()
form.SetText(inputPlace, text)
form.SetBackgroundColor("-", backgroundColor)
form.SetFontSize("-", fontSize)
return form.Response()
}
func getHtml(this js.Value, args []js.Value) interface{} {
return "<marquee>Tag From Wasm!</marquee>"
}
func main() {
addFunc := js.FuncOf(add)
setDataFunc := js.FuncOf(setData)
getHtmlFunc := js.FuncOf(getHtml)
js.Global().Set("add", addFunc)
js.Global().Set("setData", setDataFunc)
js.Global().Set("getHtml", getHtmlFunc)
select {}
}The methods that should be called from JavaScript must be registered in the JavaScript global scope using js.Global().Set.
For example:
js.Global().Set("add", addFunc)makes the Go method available as the JavaScript function add.
The js.Func values are retained in local variables within main, which remains active while the WebAssembly module is running. This keeps the registered callbacks available for JavaScript calls.
The select {} statement prevents the Go program from exiting after the functions have been registered.
Go provides WebAssembly support through the GOOS=js and GOARCH=wasm build targets.
The following command builds the Go WebAssembly module:
GOOS=js GOARCH=wasm go build -o webforms-go.wasm
On Windows, the following build.bat file can be used:
@echo off
set GOOS=js
set GOARCH=wasm
go build -o webforms-go.wasm
pauseThe resulting files can be placed in the WebForms Core application as follows:
/web-assembly/go/
webforms-go.wasm
wasm_exec.js
The wasm_exec.js file is part of the Go WebAssembly runtime. It is required to initialize and execute Go WebAssembly modules in the browser. Use the wasm_exec.js file provided by the Go installation corresponding to the Go version used to build the module.
WebForms Core can use this runtime when executing a Go WebAssembly module, provided that the application is configured to load it from the specified location.
The following CodeBehind controller uses the Go WebAssembly module.
using CodeBehind;
public partial class WasmGOController : CodeBehindController
{
public void PageLoad(HttpContext context)
{
string WasmPath = "/web-assembly/go/webforms-go.wasm"; // or wasm_exec.js
WebForms form = new WebForms();
form.AddText("<b>", Fetch.WasmMethod(WasmLanguage.GO, WasmPath, "add", [10000, 3]));
form.SetWasmEvent("WasmEvent", HtmlEvent.OnClick, WasmLanguage.GO, WasmPath, "setData", ["h3Tag", "Text From Wasm", "lightgreen", "30px"]);
form.SetWasmEvent("WasmEventWithOutput", HtmlEvent.OnClick, WasmLanguage.GO, WasmPath, "getHtml", [], "WasmHtmlOutput");
Write(form.ExportToHtmlComment());
}
}The Fetch.WasmMethod method calls a Go WebAssembly method and returns its result.
For example:
Fetch.WasmMethod(
WasmLanguage.GO,
WasmPath,
"add",
[10000, 3]
)calls the add method inside the Go WebAssembly module with the values 10000 and 3. The result is then added to the WebForms Core response.
The SetWasmEvent method associates a Go WebAssembly method with a browser event.
For example:
form.SetWasmEvent(
"WasmEvent",
HtmlEvent.OnClick,
WasmLanguage.GO,
WasmPath,
"setData",
["h3Tag", "Text From Wasm", "lightgreen", "30px"]
);When the WasmEvent button is clicked, the setData method in the Go WebAssembly module is executed.
The method creates a WebForms instance and generates WebForms Core commands that modify the h3Tag element. These commands are returned by form.Response() for processing by WebFormsJS.
The second SetWasmEvent call executes the getHtml method and places its returned HTML into the WasmHtmlOutput element.
The CodeBehind controller can be used with the following view:
@page
@controller WasmGOController
@layout "/layout.aspx"
@{
ViewData.Add("title","GO Wasm");
}
<h3>GO Wasm</h3>
<b>GO WASM Result: </b>
<br>
<button id="WasmEvent">Wasm Event</button>
<br>
<h3 id="h3Tag">Wasm Tag Changing!</h3>
<button id="WasmEventWithOutput">Wasm Event With Output</button>
<p id="WasmHtmlOutput">Wasm Html Output</p>The page contains two buttons.
The first button executes the setData method from the Go WebAssembly module. The method generates WebForms Core commands that change the text, background color, and font size of the h3Tag element.
The second button executes the getHtml method. The returned HTML is placed inside the WasmHtmlOutput element.
In this model, Go WebAssembly does not directly manipulate the browser DOM.
The Go module generates either a value or a WebForms Core response. WebFormsJS receives the generated commands and executes them against the HTML DOM.
The execution flow is:
CodeBehind
↓
WebForms
↓
Go WebAssembly
↓
WebForms Core Response
↓
WebFormsJS Executor
↓
HTML DOM
The Go WebAssembly runtime is initialized through wasm_exec.js.
The Go methods are exposed to JavaScript through the syscall/js package, while the WebForms Core WebForms class is used to generate UI commands.
This allows Go WebAssembly modules to participate in WebForms Core applications without requiring the Go module to implement browser DOM manipulation.
After learning this example, you can use the WebForms Core WebForms class inside Go WebAssembly modules to perform calculations, generate WebForms Core responses, and create more advanced server and browser interactions.
WebForms Core uses WebFormsJS as its browser-side Executor.
The WebFormsJS script is included in the HTML page:
<script type="module" src="/script/web-forms.js"></script>WebFormsJS receives the commands generated by the Commander and executes them in the browser.
The latest WebFormsJS Executor is available through npm or as a downloadable JavaScript file.
Installation via Package
npm package: webformsjs
npm install webformsjsDownload / Copy the File
WebForms Core is released under the MIT License.
