# gosfc: Go in Vue and Astro

> Write <script setup lang="go"> in a .vue file, or ---go frontmatter in an .astro file. It is real Go, compiled by goesm to plain JavaScript; Vue and Astro render it, and Vite builds it.

[Get started](https://goesm.dev/gosfc/guide/getting-started.md) · [What is gosfc?](https://goesm.dev/gosfc/guide.md) · [GitHub](https://github.com/goesm-dev/gosfc)

- [**Real Go**](https://goesm.dev/gosfc/guide/go-block.md): The Go block imports ordinary Go packages from your go.mod. gopls, go vet and go test keep working on them.
- **Still a Vue SFC**: The template, styles and the rest of the file are left to Vue. Components with lang="ts" keep working alongside.
- [**Go in .astro files**](https://goesm.dev/gosfc/guide/astro.md): One integration for Astro. The frontmatter of an .astro file can be Go, <script lang="go"> runs Go in the browser, and Vue components render on the server and hydrate as islands.
- [**No WebAssembly**](https://goesm.dev/gosfc/guide/benchmark.md): goesm compiles the Go to ES modules. The client JS of the benchmark page is 13% larger (gzip) than the same components in TypeScript.
- [**Errors point at your file**](https://goesm.dev/gosfc/reference/architecture.md): Go's diagnostics, the source maps and the stack trace of a panic all point at lines of the .vue or .astro file.
- [**Hot reload**](https://goesm.dev/gosfc/reference/architecture.md): A template edit re-renders and keeps the Go state; a Go edit reloads the component, and so does editing a .go file it uses.

## A component, in Go

```vue
<template>
  <button type="button" @click="Increment">Clicked {{ count }} times</button>
</template>

<script setup lang="go">
count := 0

func Increment() {
	count++
}
</script>
```

The block runs once per component instance, like Vue's `<script setup>`. Its
top-level variables and functions are what the template sees, and the page
updates when a Go function changes them.

## A page, in Go

```astro
---go
import (
	"strconv"

	cart "example.com/app/src/features/cart/pkg"
	Summary "../features/cart/Summary.vue"
)

items := []cart.Item{{Price: 120, Quantity: 3}, {Price: 80, Quantity: 1}}
total := cart.Total(items)

func Yen(n int) string {
	return "¥" + strconv.Itoa(n)
}
---

<p>Total: {Yen(total)} ({items.length} items)</p>
<Summary />
```

An `.astro` file whose frontmatter opens with `---go` is Go, too. It runs once
per render, at build time or per request, and the template sees its values and
functions. A `<script lang="go">` in the template runs Go in the browser
([Go in .astro files](https://goesm.dev/gosfc/guide/astro.md)).

## Who does what

gosfc only connects tools that already exist: goesm compiles Go, the Vue
tooling compiles SFCs and templates, Astro compiles `.astro` files and renders
pages, and Vite builds.
[The architecture](https://goesm.dev/gosfc/reference/architecture.md) has the details.
