Multi typed entities | 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)  Multi typed entities

 Version   v1      

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

  Multi typed entities    
- [ 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)

 Multi typed entities
====================

Schema.org allows [multi typed entities](https://github.com/schemaorg/schemaorg/wiki/How-to-use-Multi-Typed-Entities-or-MTEs). Use `MultiTypedEntity`, which works similarly to the graph.

```
package main

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

func main() {
    entity := schema.NewMultiTypedEntity()
    entity.HotelRoom().Name("The Presidential Suite")
    entity.Product().Offers(schema.Offer().
        Name("One Night").Price(100000).PriceCurrency("USD"))
    entity.Product().AggregateRating(schema.AggregateRating().
        BestRating(5).WorstRating(4))

    data, err := entity.ToJSON()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(data))
}
```

The result is one object with `@type: ["HotelRoom", "Product"]`, the room's name, the product's offers and its aggregate rating. Components are live pointers.

Properties are merged in component insertion order. Later types win conflicts, so avoid defining the same property on different components unless that is what you want. Replacing an existing component with `Set` preserves its position.

`Add` rejects duplicate types with `ErrAlreadyExists`. `Set` inserts or replaces a type. `Has`, `Get`, `GetOrCreate` and `GetNodes` work like their graph counterparts without local keys. There is no visibility filter, because every component contributes to the single entity. `SetNonce` and `When` are available as well.

An empty multi-typed entity renders `{"@context":"https://schema.org","@type":[]}`. It can be nested inside another entity or rendered using `ToArray`, `ToJSON`, `json.Marshal` and `ToScript`.

 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)
