Standard library / encoding/json

encoding/json.Encoder.Encode()

Write the JSON representation of a value followed by a newline. Return serialization or writer errors; multiple successful calls form a value stream. Expected stdout: '1\n2\n'.

Formal Syntax & Structure

go syntaxgo
func (enc *Encoder) Encode(v any) error

Parameters & Attributes

NameTypeDescription
vanyGo value to encode or destination pointer to decode, depending on the operation.

Return Value

error

Nil on success; otherwise an operation-specific error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/json"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  e := json.NewEncoder(&b)  must(e.Encode(1))  must(e.Encode(2))  fmt.Print(b.String())}
Go Reference