Escaping an apostrophe in golang

go

Solution

Escaping a character is only necessary if it can be interpreted in two or more ways. The apostrophe in your string can only be interpreted as an apostrophe, escaping is therefore not necessary as such. This is probably why you see the error message `unknown escape sequence: '`.

If you need to escape the apostrophe because it is inserted into a database, first consider using library functions for escaping or inserting data directly. Correct escaping has been the culprit of many security problems in the last decades. You will almost certainly do it wrong.

Having said that, you have to escape `\` to do what you want (click to play):

fmt.Println("\\'") # outputs \'

As you're using cassandra, you can use packages like gocql which provide you with parametrized queries:

session.Query(`INSERT INTO sometable (text) VALUES (?)`, "'escaping'").Exec();

Problem

How can I escape an apostrophe in golang? I have a string ``` s = "I've this book" ``` and I want to make it ``` s = "I\'ve this book" ``` How to achieve this? Thanks in advance.

Original source