# Writing the Go block

> What a <script setup lang="go"> block can contain, how the template sees its variables and functions, and how it receives props.

* Like Vue's `<script setup>`, the top level runs once, top to bottom, per component instance. You can write `x := ...`, `var`, `const`, `type`, and `func F() {...}`.
* Top-level variables, constants, and functions are available in the template. When the template calls a Go function (for example `@click="Increment"`), the rendered output follows the Go values. Values and the arguments and results of calls are converted as for goesm's [exported functions](https://github.com/goesm-dev/goesm/blob/main/docs/js-exports.md): strings, slices as arrays, structs as plain objects.
* `import` takes Go packages only; the Go frontmatter of an `.astro` file can also import JavaScript modules, as described below. In a Vue component, Vue components, TypeScript and JavaScript come in through goesm's [`//goesm:import`](https://github.com/goesm-dev/goesm/blob/main/docs/js-imports.md) directive, before a `var` (a component, a class, any value) or a function without a body (a function, called with automatic conversion of its arguments and result). Both are template bindings like the block's other names. Their types may use imported and built-in types, not types declared in the block, since they are declared at package level.

  ```vue
  <template>
    <p>{{ formatPrice(12800) }} <Badge label="new" /></p>
  </template>

  <script setup lang="go">
  import "syscall/js"

  //goesm:import "./Badge.vue"
  var Badge js.Value

  //goesm:import "./format.ts" formatPrice
  func formatPrice(yen int) string
  </script>
  ```
* Put methods and generic functions in a Go package.
* To receive props, declare `type Props struct {...}`; the block then has a `props` variable of that type. Field `Route` is read from the attribute `route` (or the field's json tag name, or its kebab-case form), converted to the field's type: strings, bools and numbers, and structs, slices, pointers (`null` is nil) and functions as goesm's ABI converts them. A field's `default:"..."` tag fills a prop the parent leaves out. When the parent passes new props, `props` is replaced and the template reads it again (`{{ props.Title }}`). The fields are declared as Vue props, so other attributes (`class`, `style`, listeners) fall through to the root element as usual.

  ```vue
  <script setup lang="go">
  import "strings"

  type Props struct {
  	Title string
  }

  heading := strings.ToUpper(props.Title)
  </script>
  ```

## State and events

Variables the block declares are the component's state. A template event that
calls a Go function runs it, and the template then shows the new values:

```vue
<template>
  <button type="button" @click="Increment">Clicked {{ count }} times</button>
  <input :value="name" @input="Rename($event.target.value)" />
  <p>Hello, {{ name }}</p>
</template>

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

func Increment() {
	count++
}

func Rename(s string) {
	name = s
}
</script>
```

The template gets a copy of each value, converted to JavaScript (a slice
becomes an array, a struct an object). Changing that copy in JavaScript does
not change the Go variable: change Go state by calling a Go function.
