print pointer value to json...

44 views
Skip to first unread message

eric

unread,
Oct 19, 2016, 9:31:32 PM10/19/16
to golang-nuts
hi, i am newer in golang.

i want to marshal pointer value to json.

but, i can't. how can i do that ?

thanks,


package main

import (
"encoding/json"
"fmt"
)

type KV struct {
k string
v string
}

func main() {
kv := []*KV{}

obj := new(KV)
obj.k = "eric"
obj.v = "23"
kv = append(kv, obj)

obj = new(KV)
obj.k = "robot"
obj.v = "32"

kv = append(kv, obj)

fmt.Printf("%d\n", len(kv))
fmt.Printf("%v\n", kv)

b, _:= json.MarshalIndent(kv, "", "  ")
fmt.Println(string(b)) // <--- not work,,,
}

output: 
2
[0x1050e140 0x1050e150]
[
  {},
  {}
]

Pietro Gagliardi

unread,
Oct 19, 2016, 10:24:00 PM10/19/16
to eric, golang-nuts
You must export the struct fields in order for package json to see them.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

abi...@gmail.com

unread,
Oct 20, 2016, 12:53:06 AM10/20/16
to golang-nuts
Exported fields must use uppercase of capital letter.

type KV struct {
  K string
  V string
Reply all
Reply to author
Forward
0 new messages