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) *EncoderParameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Destination 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())}