Encode writes a TOML representation of v to the stream. If v cannot be represented to TOML it returns an error. # Encoding rules A top level slice containing only maps or structs is encoded as [[table array]]. All slices not matching rule 1 are encoded as [array]. As a result, any map or struct
(v interface{})
| 184 | // inside inline tables. For array tables, the comment is only present before |
| 185 | // the first element of the array. |
| 186 | func (enc *Encoder) Encode(v interface{}) error { |
| 187 | e := encoderStatePool.Get().(*encoderState) |
| 188 | e.Encoder = enc |
| 189 | e.buf = e.buf[:0] |
| 190 | e.keyStack = e.keyStack[:0] |
| 191 | e.lastWasHeader = false |
| 192 | |
| 193 | err := e.encodeRoot(v) |
| 194 | if err != nil { |
| 195 | encoderStatePool.Put(e) |
| 196 | return err |
| 197 | } |
| 198 | |
| 199 | _, err = enc.w.Write(e.buf) |
| 200 | encoderStatePool.Put(e) |
| 201 | if err != nil { |
| 202 | return fmt.Errorf("toml: cannot write: %w", err) |
| 203 | } |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | var encoderStatePool = sync.Pool{ |
| 208 | New: func() interface{} { return &encoderState{} }, |