从Go结构创建JSON表示形式

I am trying to create a JSON string from a struct:

package main

import "fmt"

func main() {
    type CommentResp struct {
        Id   string `json: "id"`
        Name string `json: "name"`
    }

    stringa := CommentResp{
        Id:   "42",
        Name: "Foo",
    }

    fmt.Println(stringa)
}

This code prints {42 foo}, but I expected {"Id":"42","Name":"Foo"}.

What you are printing is fmt's serialization of the CommentResp struct. Instead, you want to do is use json.Marshal to get the encoded JSON repsentation:

data, err := json.Marshal(stringa)
if err != nil {
     // Problem encoding stringa
     panic(err)
}
fmt.Println(string(data))

https://play.golang.org/p/ogWKQ3M6tb

Also, your json struct tags are not valid; there cannot be a space between : and the quoted string:

type CommentResp struct {
    Id   string `json:"id"`
    Name string `json:"name"`
}

https://play.golang.org/p/eQiyTk6-vQ