将JSON转换为字符串错误字符串文字未终止[重复]

This question already has an answer here:

When I try to run given code I get the error :: string literal not terminated (illegal character U+005C '\') . How do I fix the given code?

payload := "{
    \"key_id\":\"3\",
    \"contacts\":[
        {
            \"external_id\":\"chandan4u1990@gmail.com\",
            \"data\":{
                \"global\":{
                    \"name\":\"Adoni Mishra\"
                }
            }
        },
        {
            \"external_id\":\"chandankumarc@airasia.com\",
            \"data\":{
                \"global\":{
                    \"name\":\"CHANDAN KUMAR\"
                }
            }
        }
    ]
}"

When I concat all the lines in one then it starts working::

payload := "{\"key_id\":\"3\",\"contacts\":[{\"external_id\":\"chandan4u1990@gmail.com\",\"data\":{\"global\":{\"name\":\"Adoni Mishra\"}}},{\"external_id\":\"chandankumarc@airasia.com\",\"data\":{\"global\":{\"name\":\"CHANDAN KUMAR\"}}}]}"
</div>

You are using an interpreted string literal which may not contain newlines! Spec: String literals:

Interpreted string literals are character sequences between double quotes, as in "bar". Within the quotes, any character may appear except newline and unescaped double quote.

Use a raw string literal so you don't even have to escape quotes, it will be much more readable, and newlines are allowed in raw string literals:

Raw string literals are character sequences between back quotes, as in foo. Within the quotes, any character may appear except back quote.

For example:

    payload := `{
    "key_id":"3",
    "contacts":[
        {
            "external_id":"chandan4u1990@gmail.com",
            "data":{
                "global":{
                    "name":"Adoni Mishra"
                }
            }
        },
        {
            "external_id":"chandankumarc@airasia.com",
            "data":{
                "global":{
                    "name":"CHANDAN KUMAR"
                }
            }
        }
    ]
}`

You can also put everything in one line if you don't need indentation:

payload := `{"key_id":"3","contacts":[{"external_id":"chandan4u1990@gmail.com","data":{"global":{"name":"Adoni Mishra"}}},{"external_id":"chandankumarc@airasia.com","data":{"global":{"name":"CHANDAN KUMAR"}}}]}`

Try it on the Go Playground.