Skip to content

Instantly share code, notes, and snippets.

@cameronjacobson
Last active August 29, 2015 13:56
Show Gist options
  • Save cameronjacobson/9015638 to your computer and use it in GitHub Desktop.
Save cameronjacobson/9015638 to your computer and use it in GitHub Desktop.
simple example of how one could wrap their structs for json en/decoding
package main
import (
"encoding/json"
"fmt"
)
type blah struct {
Numbers []int
Astring string
}
func (self *blah) fromJson(data string) {
err := json.Unmarshal([]byte(data), self)
if err != nil {
fmt.Println("error:", err)
}
}
func (self *blah) toJson() string {
data, err := json.Marshal(self)
if err != nil {
fmt.Println("error:", err)
}
return string(data)
}
func main() {
ab := `{
"Numbers": [1, 2, 3],
"Astring": "this is the string"
}`
var ac blah
ac.fromJson(ab)
fmt.Println(ac)
fmt.Println(ac.toJson())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment