Usage | schema-org-go | Spatie       

 SPATIE  

schema-org-go
=============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Schema-org-go](https://spatie.be/docs/schema-org-go/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/schema-org-go/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/schema-org-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/schema-org-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/schema-org-go/v1/usage)
- [ Advanced usage ](https://spatie.be/docs/schema-org-go/v1/advanced-usage)
- [ Graph - multiple items ](https://spatie.be/docs/schema-org-go/v1/graphs)
- [ Multi typed entities ](https://spatie.be/docs/schema-org-go/v1/multi-typed-entities)
- [ Generating types ](https://spatie.be/docs/schema-org-go/v1/generating-types)
- [ Testing ](https://spatie.be/docs/schema-org-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/schema-org-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/schema-org-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/schema-org-go/v1/support-us)

 Usage
=====

###  On this page 

1. [ Enumerations ](#content-enumerations)
2. [ Identifier ](#content-identifier)

```
package main

import (
    "fmt"
    schema "github.com/spatie/schema-org-go"
)

func main() {
    business := schema.LocalBusiness().Name("Spatie").
        ContactPoint(schema.ContactPoint().AreaServed("Worldwide"))
    business.SameAs([]string{"https://spatie.be", "https://github.com/spatie"})
    business.When(true, func(entity *schema.Entity) {
        entity.Email("info@spatie.be")
    })

    script, err := business.SetNonce("your-csp-nonce").ToScript()
    if err != nil {
        panic(err)
    }
    fmt.Println(script)
}
```

All types can be instantiated through the package functions, such as `schema.LocalBusiness()`, or with `schema.New("LocalBusiness")`. Both return `*schema.Entity`. All properties accept slices of the expected data type. For example, `SameAs` accepts a string or a slice of strings.

Types can be converted to a normalized map with `ToArray`, encoded as JSON bytes with `ToJSON`, or rendered as a script with `ToScript`. Each returns an error, so encoding failures do not become empty or partial scripts. `json.Marshal(entity)` also works. Unlike PHP, there is no implicit string conversion to HTML.

JSON object keys are sorted by Go's encoder. Nested documents have their `@context` stripped. `time.Time` values render as ISO 8601 with seconds and a numeric timezone offset. `fmt.Stringer` and `json.Marshaler` values are supported. Other struct values, functions, channels and non-finite numbers return an error. String-keyed maps and slices are traversed recursively.

Nil and empty strings are ignored without overwriting a previous value. False, zero, empty slices and empty maps are retained. To remove a property, use `RemoveProperty` rather than assigning nil. PHP array-access operations correspond to `HasProperty`, `GetProperty`, `SetProperty` and `RemoveProperty`.

Enumerations
--------------------------------------------------------------------------------------------

All enumeration values are available as package constants prefixed with their type. For example, use `schema.Book().BookFormat(schema.BookFormatTypeHardcover)`.

Identifier
--------------------------------------------------------------------------------------

The `identifier` key is replaced by `@id` for simple identifiers. A typed identifier remains under `identifier`. If both a simple `identifier` and `@id` are assigned, the simple identifier wins. Rendering does not mutate the original properties.

Use `entity.Referenced()` to include only the entity's current `@id`. Identifiers assigned later are resolved at render time. References retain their targets through Go's garbage collector, so circular reference links do not require manual lifetime management. A missing identifier renders as `{"@id":null}`. Calling `ToScript` on a reference renders its complete target, matching PHP.

I recommend double checking your structured data with [Google's rich results test](https://search.google.com/test/rich-results).

Continue with [advanced usage](advanced-usage), [graphs](graphs) and [multi-typed entities](multi-typed-entities).

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
