Last active
August 27, 2021 14:06
-
-
Save johanmcos/e1d640f456bdc94371c63a1d808b158d to your computer and use it in GitHub Desktop.
example of JSON encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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