Go Server Pages, or "Gosp" for short, let you embed Go code right in the middle of an HTML document—or in fact any type of document that your Web server can serve. The Web server executes the code and includes its output in the page, right where the code originally appeared. The process is completely invisible to clients, making Go Server Pages 100% compatible with every Web browser in existence.
If you're familiar with PHP or JavaServer Pages, Go Server Pages is just like those but based on the Go programming language.
Here's a trivial example of a Go Server Page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My first Go Server Page</title>
</head>
<body>
<p>
I like the number <?go:expr 2*2*2*3*3*5*7 ?> because it's
divisible by each integer from 1 to 10.
</p>
</body>
</html>
The only difference from ordinary HTML in the above is the <?go:expr 2*2*2*3*3*5*7 ?>
markup. This tells the Web server to evaluate 2*2*2*3*3*5*7
as a Go expression and replace the markup with the result, producing
I like the number 2520 because it's divisible by each integer from 1 to 10.
in the HTML sent back to the user's Web browser. The user has no way of knowing that 2520
was generated dynamically, not entered statically in the HTML.
Go Server Pages is designed for simplicity. Only the following new markup is introduced:
Markup | Meaning |
---|---|
<?go:expr code ?> | Evaluate code as a Go expression |
<?go:block code ?> | Execute statement or statement block code |
<?go:top code ?> | Declare file-level code code (import , func , const , etc.) |
<?go:include file ?> | Include local file file as if it were pasted in |
Not surprisingly, Go Server Pages requires a Go installation. Go Server Pages currently works only with the Apache Web server.
Full documentation can be found at https://gosp.pakin.org/.