9951 explained code solutions for 126 technologies


golangMarshal example


package main
import "encoding/json"

type President struct {
  Name string
  Age int
}

func main() {
  p := President{Name: "Joe", Age: 99}
  res, _ := json.Marshal(p)
}ctrl + c
package main

default package declaration

"encoding/json"

lib to work with JSON

type President struct

defines struct with fields

President

struct name

json.Marshal

converts given variable (President struct in our case) to JSON


Usage example

package main
import "encoding/json"

type President struct {
  Name string
  Age int
}

func main() {
  p := President{Name: "Joe", Age: 99}
  res, _ := json.Marshal(p)
  print(string(res))
}
output
{"Name":"Joe","Age":99}