编组json.RawMessage返回base64编码的字符串

I run the following code:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    raw := json.RawMessage(`{"foo":"bar"}`)
    j, err := json.Marshal(raw)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(j))  
}

Playground: http://play.golang.org/p/qbkEIZRTPQ

Output:

"eyJmb28iOiJiYXIifQ=="

Desired output:

{"foo":"bar"}

Why does it base64 encode my RawMessage as if it was an ordinary []byte?

After all, RawMessage's implementation of MarshalJSON is just returning the byte slice

// MarshalJSON returns *m as the JSON encoding of m.
func (m *RawMessage) MarshalJSON() ([]byte, error) {
    return *m, nil 
}

Found the answer in a go-nuts thread

The value passed to json.Marshal must be a pointer for json.RawMessage to work properly:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    raw := json.RawMessage(`{"foo":"bar"}`)
    j, err := json.Marshal(&raw)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(j))  
}