Standard library / encoding/json

encoding/json.NewEncoder()

Construct a JSON encoder writing to an io.Writer. Encode emits one value followed by a newline. Expected stdout: '"7\\n"\n'.

Formal Syntax & Structure

go syntaxgo
func NewEncoder(w io.Writer) *Encoder

Parameters & Attributes

NameTypeDescription
wio.WriterDestination writer.

Return Value

*Encoder

New streaming JSON encoder.

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(7))  fmt.Printf("%q\n", b.String())}
Go Reference