I have been trying to store a large string into a string variable in GoLang , but for some unknown reason GoLang is limiting the string to 64 Bytes in length
The main purpose of this string concatenation is to generate a couchbase's N1QL query at runtime based on user input
userInput := []string{"apple", "boy", "cat", "dog"}
var buffer string
buffer = "SELECT * FROM DB WHERE DB.ITEM_NAME="+userInput[0]+
"OR DB.ITEM_NAME="+userInput[1]
In such a case if I debug on variable buffer, for example I can see it contains only until "SELECT * FROM DB WHERE DB.ITEM_NAME="+userInput[0]+OR" depending upon user input size it varies and it caps the string to 64th character
The behaviour is as expected. The behaviour is not strange.
Your code creates obviously wrong Couchbase N1QL:
package main
import (
"fmt"
)
func main() {
userInput := []string{"apple", "boy", "cat", "dog"}
var buffer string
buffer = "SELECT * FROM DB WHERE DB.ITEM_NAME=" + userInput[0] +
"OR DB.ITEM_NAME=" + userInput[1]
fmt.Println(buffer)
}
Output:
SELECT * FROM DB WHERE DB.ITEM_NAME=appleOR DB.ITEM_NAME=boy
Here is a plausible solution:
package main
import (
"fmt"
)
func main() {
userInput := []string{"apple", "boy", "cat", "dog"}
query := fmt.Sprintf(
`SELECT * FROM DB WHERE DB.ITEM_NAME=%q OR DB.ITEM_NAME=%q;`,
userInput[0], userInput[1],
)
fmt.Println(query)
}
Output:
SELECT * FROM DB WHERE DB.ITEM_NAME="apple" OR DB.ITEM_NAME="boy";
Note: Beware of SQL injection.
References:
The Go Programming Language Specification