# gosfc: Vue と Astro に Go を

> .vue ファイルには <script setup lang="go"> を、.astro ファイルには ---go のフロントマターを書きます。中身は本物の Go で、goesm が素の JavaScript にコンパイルし、Vue と Astro が描画し、Vite がビルドします。

[はじめに](https://goesm.dev/gosfc/ja/guide/getting-started.md) · [gosfc とは](https://goesm.dev/gosfc/ja/guide.md) · [GitHub](https://github.com/goesm-dev/gosfc)

- [**本物の Go**](https://goesm.dev/gosfc/ja/guide/go-block.md): Go ブロックは go.mod にある普通の Go のパッケージを import します。gopls、go vet、go test もそのまま使えます。
- **Vue の SFC のまま**: テンプレート、スタイル、ファイルの残りの部分は Vue に任せます。lang="ts" のコンポーネントも並べて使えます。
- [**.astro ファイルにも Go**](https://goesm.dev/gosfc/ja/guide/astro.md): Astro にはインテグレーション 1 つで組み込めます。.astro ファイルのフロントマターを Go で書け、<script lang="go"> はブラウザで Go を実行します。Vue コンポーネントはサーバーで描画されてアイランドとしてハイドレートします。
- [**WebAssembly なし**](https://goesm.dev/gosfc/ja/guide/benchmark.md): goesm が Go を ES モジュールにコンパイルします。ベンチマークのページのクライアント JS は、同じコンポーネントを TypeScript で書いた場合より 13% 大きいだけです (gzip)。
- [**エラーは書いたファイルを指す**](https://goesm.dev/gosfc/ja/reference/architecture.md): Go の診断メッセージ、ソースマップ、panic のスタックトレースは、すべて .vue や .astro ファイルの行を指します。
- [**ホットリロード**](https://goesm.dev/gosfc/ja/reference/architecture.md): テンプレートの編集は Go の状態を保ったまま再描画し、Go の編集はコンポーネントを読み直します。使っている .go ファイルの編集も同じです。

## Go で書いたコンポーネント

```vue
<template>
  <button type="button" @click="Increment">{{ count }} 回クリックしました</button>
</template>

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

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

Go ブロックは、Vue の `<script setup>` と同じくコンポーネントのインスタンスごとに 1 回
実行されます。トップレベルの変数と関数がテンプレートから見え、Go の関数がそれを変えると
ページが更新されます。

## 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>合計: {Yen(total)} ({items.length} 品目)</p>
<Summary />
```

フロントマターを `---go` で始めた `.astro` ファイルも Go で書けます。フロントマターは
ビルド時またはリクエストごとに、描画のたびに 1 回実行され、その値と関数をテンプレートから
使えます。テンプレートの中の `<script lang="go">` は、ブラウザで Go を実行します
([.astro ファイルで Go を使う](https://goesm.dev/gosfc/ja/guide/astro.md))。

## 役割分担

gosfc は既存のツールをつなぐだけです。Go のコンパイルは goesm、SFC とテンプレートの
コンパイルは Vue のツール、`.astro` ファイルのコンパイルとページの描画は Astro、ビルドは
Vite が担当します。
詳しくは [アーキテクチャ](https://goesm.dev/gosfc/ja/reference/architecture.md) を見てください。
