Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

more than one character in rune literal

Tags:

go

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)
like image 543
Ninja Avatar asked Dec 07 '22 09:12

Ninja


2 Answers

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)
like image 186
Erwin Bolwidt Avatar answered Dec 25 '22 05:12

Erwin Bolwidt


Issue here is single quotes . Go Compiler expects a character only when encounters '' . Rather use double quotes with escape symbol as explained in above example.

like image 21
remote007 Avatar answered Dec 25 '22 04:12

remote007