more than one character in rune literal

Issue

I have a string as just MyString and I want to append in this data something like this:

MYString ("1", "a"), ("1", "b")  //END result 

My code is something like this:

    query := "MyString"; 
    array := []string{"a", "b"}
    
    for i , v :=  range array{
        id := "1" 
        fmt.Println(v,i)
        query +=  '("{}", "{}"), '.format(id, v)
     }

but I am getting two errors:

./prog.go:15:23: more than one character in rune literal
./prog.go:15:39: '\u0000'.format undefined (type rune has no field or method format)

Solution

You can’t use single quotes for Strings in Go. You can only use double-quotes or backticks.
Single quotes are used for single characters, called runes

Change your line to:

query +=  "(\"{}\", \"{}\"), ".format(id, v)

or

 query +=  `("{}", "{}"), `.format(id, v)

However, Go is not python. Go doesn’t have a format method like that. But it has fmt.Sprintf.

So to really fix it, use:

query = fmt.Sprintf(`%s("%s", "%s"), `, query, id, v)

Answered By – Erwin Bolwidt

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.