Yi Jiang
Yi Jiang

Reputation: 4028

In golang, what is nice format to write JSON and MySQL query string?

It is really hard to read an extremely long string of JSON or DB query in golang source code.

JSON Example:

"{\"task\":\"send_sms\",\"t_id\":988110,\"req\":{\"node\":1,\"msg_id\":987654321,\"m_num\":\"+61433092888\",\"p_num\":\"Private\",\"msg\":\"Hello world.\"}}      "

DB Query Example:

dbQuery = fmt.Sprintf("INSERT INTO `ms_message` (`task_id`, `panel_phone_num` ,`user_mobile_num`,`message_content`,`receiver_node_id`,`modem_index`,`generate_datetime` ) SELECT * FROM (SELECT %d, \"%s\", \"%s\", \"%s\", %d,%d,\"%s\") AS tmp WHERE NOT EXISTS ( SELECT * FROM `ms_message` WHERE `receiver_node_id` = %d AND `task_id` = %d ) LIMIT 1", taskId, panelNumber, mobileNumber, messageContent, nodeId, modemId, dateTime, nodeId, taskId)

What way is nice to make above strings looks reader friendly?

Upvotes: 2

Views: 991

Answers (1)

pkaeding
pkaeding

Reputation: 37643

You can use backticks to write a multiline string literal, like so:

json := `
{
    "hello": "world",
    "foo": "bar"
}`

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

The backticks in your SQL example might be problematic, though...

Upvotes: 2

Related Questions