Skip to content

Instantly share code, notes, and snippets.

@johanmcos
Last active August 27, 2021 14:06
Show Gist options
  • Save johanmcos/e1d640f456bdc94371c63a1d808b158d to your computer and use it in GitHub Desktop.
Save johanmcos/e1d640f456bdc94371c63a1d808b158d to your computer and use it in GitHub Desktop.
example of JSON encoding
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
)
const appVersion string = "v1"
type bodyParams struct {
AppVersion string `json:"app_version"`
}
func main() {
newBody := bodyParams{AppVersion: appVersion}
bodyJSON, err := json.Marshal(newBody)
if err != nil {
panic(err)
}
encodedJSON := base64.StdEncoding.EncodeToString(bodyJSON)
fmt.Println("encoded JSON is", encodedJSON)
decodedJSON, err := base64.StdEncoding.DecodeString(encodedJSON)
if err != nil {
panic(err)
}
body := &bodyParams{}
err = json.Unmarshal(decodedJSON, body)
if err != nil {
panic(err)
}
fmt.Println("decoded JSON is", *body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment